我需要找到所有div
s中的所有p标签和一类someClass
并用另一个div
包装它们。这是开头标记的样子:
<div class="someClass">
// Lots of different tags generated by the site
<p>Some text</p>
<p>Some text</p>
<p>Some text</p>
<p>Some text</p>
</div>
<div class="someClass">
// Lots of different tags generated by the site
<p>Some text</p>
<p>Some text</p>
</div>
会变成:
<div class="someClass">
// Lots of different tags generated by the site
<div class="bla">
<p>Some text</p>
<p>Some text</p>
<p>Some text</p>
<p>Some text</p>
</div>
</div>
<div class="someClass">
// Lots of different tags generated by the site
<div class="bla">
<p>Some text</p>
<p>Some text</p>
</div>
</div>
有任何想法吗?当我尝试使用.each()
:为每个div
与一类someClass
包装所有p
标签,但它只是将它们全部包裹在顶部div
。
你试过这个吗?
$('div.someClass p').wrapAll(...);
或这个?
$('div.someClass').each(function() {
$(this).find('p').wrapAll(...);
});
编辑
查看您发布的代码后,似乎是一个语法问题。你需要这行中的引号:
$(this).find('p').wrapAll(<div class='toggle'></div>);
它应该是:
$(this).find('p').wrapAll("<div class='toggle'></div>");