gpt4 book ai didi

javascript - jQuery load() 函数内的作用域

转载 作者:行者123 更新时间:2023-11-30 13:05:20 25 4
gpt4 key购买 nike

我的范围有问题。很抱歉这个愚蠢的问题。

我正在尝试使用 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
});

最佳答案

LIVE DEMO

$(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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com