我遇到了作用域問題。對於這個沒有問題的問題抱歉。jQuery中的作用域load()函數
我想將圖像追加到父div與jQuery,但不知道如何將變量「theParent」傳遞給加載函數。
// inside a this.each loop
var theParent = $(this).parent('div.parent');
var img = $("<img />")
.attr('src', '/path/img.jpg')
.error(function(){ console.log('error'); })
.load(function(){
img.addClass('myClass'); // <-- not working plz help
theParent.append(img); // <-- not working plz help
});
你能張貼整個this.each循環?看起來像是等待.load在下一次迭代之前完成的問題。 – ZimSystem 2013-04-08 15:19:39
這裏似乎沒有任何範圍問題。 – Quentin 2013-04-08 15:19:46
這似乎工作正常 - http://jsfiddle.net/k2Kb7/1/(雖然不知道你的循環) – Ian 2013-04-08 15:38:32