我的范围有问题。很抱歉这个愚蠢的问题。
我正在尝试使用 jQuery 将图像附加到父 div,但不知道如何将变量“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).addClass('myClass');
会工作但使用:
var img = $("<img />", {
"src" : '/images/favicon.png',
"class" : 'myClass'
})
.error(function(){ console.log('error'); })
.load(function(){
img.appendTo( theParent );
});
关于javascript - jQuery load() 函数内的作用域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15882864/