這是我得到它已被追加之後的jQuery .append元素的性質
var path = $('#sprite').css('background-image').replace('url', '').replace('(', '').replace(')', '').replace('"', '').replace('"', '')
var tempImg = '<img id="tempImg" src="' + path + ' "/>';
$('body').append(tempImg);
alert($('#tempImg').width());
結果是0
,但如果我去到一個控制檯,然後輸入$('#tempImg').width()
,我們得到正確的寬度。
那麼,什麼似乎是追加只是「裝」的代碼執行後,我需要用我的代碼項目已被追加後,有沒有辦法?
你需要等待圖像負載,有幾個答案,以及如何做到這一點很好的代碼示例。 – adeneo 2012-08-02 19:05:51
你什麼時候調用這個代碼?你試過用$(function(){...})來包裝它。 ? – Vlad 2012-08-02 19:05:53
的代碼被稱爲成$(文件)。就緒(函數(){}); – steps 2012-08-02 19:25:30