Sou*_*aby 0 html jquery append find
我试图写一些jquery,它查找div(.content)中的所有h2标签,然后将每个h2标签附加到另一个div(.intro)中。
到目前为止,我有这个:
var h2 = $(".content").find("h2");
$(h2).each(function() {
$(this).append(".intro");
});
Run Code Online (Sandbox Code Playgroud)
但这不起作用..如果有人可以帮助我,那就太好了:)
尝试这个:
var h2 = $('h2', '.content');
$('.intro').append(h2);
Run Code Online (Sandbox Code Playgroud)
无需使用each()。jQuery具有一种称为“隐式迭代”的功能,您可以在其中立即对整个集合进行操作。因此,您可以全部抓住它们,并同时附加所有它们。
还是要附加它们的副本?
如果是这样,请尝试:
var h2 = $('h2', '.content');
$('.intro').append(h2.clone());
Run Code Online (Sandbox Code Playgroud)
编辑:
或使用单行版本:
$('.intro').append($('h2', '.content').clone());?
Run Code Online (Sandbox Code Playgroud)
(当然,.clone()如果不需要复制它们,则将其删除。)