2013-12-12 94 views
-1

我有一個圖像,可以說它是由一個變量引用var IMG = $("img")我想捕獲它的原始高度和寬度。我使用下面的代碼。它運行良好,但它不適用於Win XP IE8。任何人都可以告訴我這是什麼問題?獲取圖像的原始寬度和高度

$("<img/>") 
    .attr("src", IMG.attr("src")) 
    .load(function() { 
     //Execution is not coming in this block 
     alert(this.width); 

    }); 
+0

我依稀記得一些事情,如果加載事件不觸發圖像文件被緩存。這可能嗎? – xec

+1

綁定'load'處理程序並*然後*設置'src'屬性。 – techfoobar

+0

@techfoobar - 不明白你在說什麼 – Ashwin

回答

2

你應該在所有的情況下設置srconload處理,特別是對緩存的圖片,嘗試,看看是否是有所作爲:

$("<img/>")  
    .load(function() { 
     //Execution is not coming in this block 
     alert(this.width); 

    }) 
    .attr("src", IMG.attr("src")); 
+0

它完美的工作。非常感謝。 – Ashwin

相關問題