简体   繁体   English

jQuery-克隆元素上的each()

[英]jQuery - each() on cloned element

I am trying to do a each() on a cloned element, 我正在尝试对克隆的元素做一个each()

var html = $(this).clone().html();

html.find('.class').each(function () {
    $(this).removeClass('class-to-remove');             
});

console.log(html);

but when I see var html in console then it shows the previous value, not the value after each() was done. 但是当我在控制台中看到var html ,它将显示先前的值,而不是each()完成后的值。

Please tell me how to get var where the each() was done. 请告诉我如何在完成each()位置获取var。

The return value of .html() is a string . .html()的返回值是一个字符串 You're better off not calling it at all in this case; 在这种情况下,最好不要完全调用它。 just use the return value from .clone() . 只需使用.clone()的返回值.clone()

var cloned = $(this).clone();
cloned.find('.class').each(function() {
  $(this).removeClass('whatever');
});

console.log(cloned.html());

Also note that .html() gets the contents of its operand, so the outer "shell" won't show up. 另请注意, .html()获取其操作数的内容 ,因此不会显示外部“外壳”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM