Javascript jQuery-克隆元素上的each()
我试图对克隆的元素执行Javascript jQuery-克隆元素上的each(),javascript,jquery,Javascript,Jquery,我试图对克隆的元素执行each() var html = $(this).clone().html(); html.find('.class').each(function () { $(this).removeClass('class-to-remove'); }); console.log(html); 但是,当我在控制台中看到var html时,它会显示前面的值,而不是each()完成后的值 请告诉我如何在执行each()的地方获取var。html(
each()
var html = $(this).clone().html();
html.find('.class').each(function () {
$(this).removeClass('class-to-remove');
});
console.log(html);
但是,当我在控制台中看到var html
时,它会显示前面的值,而不是each()
完成后的值
请告诉我如何在执行
each()
的地方获取var。html()的返回值是一个字符串。在这种情况下,你最好不要打电话;只需使用.clone()
中的返回值即可
还请注意,
.html()
获取其操作数的内容,因此外部“shell”不会显示。.html()
的返回值是一个字符串。在这种情况下,你最好不要打电话;只需使用.clone()
中的返回值即可
另请注意,
.html()
获取其操作数的内容,因此外部“外壳”不会显示。旁注:这里不需要.each()方法,只需调用html.find('.class').removeClass('class-to-remove')
@yoavmatchulsky内部有更多的代码。。each()
。但是谢谢,我会记住这一点。旁注:这里不需要.each()方法,只需调用html.find('.class').removeClass('class-to-remove')
@yoavmatchulsky中有更多的代码。each()
。但是谢谢,我会记住这一点。
var cloned = $(this).clone();
cloned.find('.class').each(function() {
$(this).removeClass('whatever');
});
console.log(cloned.html());