用jQuery创建的<img>元素没法获取高度和宽度,通过width()和height()获取的值为0

解决方案 »

  1.   


    <script>
    var  pic=document.createElement("img");
    pic.src="a.jpg";
    document.body.appendChild(pic);
    alert(pic.offsetHeight);
    </script>
      

  2.   

    的确width和height是0,可能只有在style里定义的height和width才能被取到
      

  3.   

    <img src="1.jpg" id="imgId" />        var img = new Image();
            img.src = $("#imgId").attr("src");
            if (img.complete) {
                alert("宽度:" + img.widht + ",高度:" + img.height);
            } else {
                img.onload = function() {
                    alert("宽度:" + this.width + ",高度:" + this.height);
                }
            }
      

  4.   

    谢谢几位!原来是img没有加载完。