<img alt="" src="http://i2.itc.cn/20130220/2d3f_a20e4149_f596_c868_3fa4_40924923debd_1.jpg" onload="imgResize(this);" id="imgid" />
由于图片有的可能比较大,因此当onload执行的时候获取不到img元素的宽高,导致宽高为0
最后执行缩放函数imgResize的时候导致执行不成功现在想知道要怎么延迟执行onload来让图片加载完成后获取到图片宽高这样就可以正常执行缩放了

解决方案 »

  1.   

    <img alt="" src="http://i2.itc.cn/20130220/2d3f_a20e4149_f596_c868_3fa4_40924923debd_1.jpg" onload="imgResize(this);" id="imgid" />
    <script type="text/javascript">
    var imgResize = function(t){
    alert( t.width )
    alert( t.height )
    }
    </script>怎么取不到呢
      

  2.   

    <img alt="" src="http://i2.itc.cn/20130220/2d3f_a20e4149_f596_c868_3fa4_40924923debd_1.jpg" onload="imgResize(this);" id="imgid" />
    <script type="text/javascript">
    function imgResize(o){
    console.log(o.width , o.height);
    }
    </script>
    试了下,可以获取到
      

  3.   

    <div class="img-box col2">
                <img alt="" src="http://i2.itc.cn/20130220/2d3f_a20e4149_f596_c868_3fa4_40924923debd_1.jpg" onload="imgResize(this);" />
            </div>
    JavaScript Code:function imgResize(imgObj) {
        //setTimeout("", 3000);
        var image = new Image();
        image.src = imgObj.src;    if (image.complete) {
            alert(image.complete); 
        }
        alert("width: " + this.width + " height: " + this.height);
    }
    环境:
    IE8
    Chrome
    宽高均是undefined
      

  4.   

    function imgResize(imgObj) {
        //setTimeout("", 3000);
        var image = new Image();
        image.src = imgObj.src;
     
        if (image.complete) {
            alert(image.complete); 
        }
    alert(this) // window?
        alert("width: " + imgObj.width + " height: " + imgObj.height);
    }
      

  5.   


    锅,请问你知道this.width的this指向的是哪个吗?你传进来的imgObj已经可以用了,为什么你还要new Image呢。如果非要new,请把alert("width: " + this.width + " height: " + this.height);这些代码放到image的onload事件中。
    function imgResize(imgObj) {
        //setTimeout("", 3000);
        var image = new Image();
        image.src = imgObj.src;
        image.onload = function(){
    alert("width: " + this.width + " height: " + this.height);
        }    
    }