我正在尝试对克隆元素执行 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()
的返回值即可。
var cloned = $(this).clone();
cloned.find('.class').each(function() {
$(this).removeClass('whatever');
});
console.log(cloned.html());
另请注意,.html()
获取其操作数的内容,因此外部“外壳”不会显示。
关于javascript - jQuery - 克隆元素上的each(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19198132/