首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在.wrapAll()中使用jQuery?

如何在.wrapAll()中使用jQuery?
EN

Stack Overflow用户
提问于 2009-10-14 15:34:37
回答 1查看 1.5K关注 0票数 3

我需要用一个div类在所有someClass中找到所有的p标记,然后用另一个div包装它们。开始标记是这样的:

代码语言:javascript
复制
<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>

会变成:

代码语言:javascript
复制
<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()时:对于每一个带有someClass类的div,都包装了所有的p标记,但是它只是将它们包装在最上面的div中。

EN

回答 1

Stack Overflow用户

发布于 2009-10-14 15:38:08

你试过这个吗?

代码语言:javascript
复制
$('div.someClass p').wrapAll(...);

还是这个?

代码语言:javascript
复制
$('div.someClass').each(function() {
  $(this).find('p').wrapAll(...);
});

编辑

在查看了您发布的代码之后,它似乎是一个语法问题。您需要这一行的引号:

代码语言:javascript
复制
$(this).find('p').wrapAll(<div class='toggle'></div>);

它应该是:

代码语言:javascript
复制
$(this).find('p').wrapAll("<div class='toggle'></div>");
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1567121

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档