开发者

How to use .wrapAll() in jQuery?

开发者 https://www.devze.com 2022-12-08 07:05 出处:网络
I need to find all the p tags inside all the divs with a class of someClass and wrap them with another div. This is how the beginning mark up would look like:

I need to find all the p tags inside all the divs with a class of someClass and wrap them with another div. This is how the beginning mark up would look like:

<div class="someClass">
  // Lo开发者_JAVA百科ts 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>

Would turn into:

<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>

Any ideas? When I try using .each(): for each div with a class of someClass wrap all the p tags, but it just wraps them all together in the top div.


Have you tried this?

$('div.someClass p').wrapAll(...);

Or this?

$('div.someClass').each(function() {
  $(this).find('p').wrapAll(...);
});

Edit

After looking at the code you posted, it appears to be a syntax issue. You need quotes in this line:

$(this).find('p').wrapAll(<div class='toggle'></div>);

It should be:

$(this).find('p').wrapAll("<div class='toggle'></div>");
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号