【问题标题】:Filter <p> within a div and wrap all在 div 中过滤 <p> 并包装所有
【发布时间】:2011-03-31 20:06:30
【问题描述】:

我正在尝试将 div 中的所有 &lt;p&gt; 包装在 sub-div 中; &lt;p&gt; 的数量不是恒定的。

<div id="page">
  <p>text</p>
  <p>text</p>
  <p>text</p>
</div>

试图获得:

<div id="page">
  <div>
    <p>text</p>
    <p>text</p>
    <p>text</p>
  </div>
</div>

使用:

<script>
 jQuery('#page').filter('p').wrapAll('<div></div>');
</script>

而且它不起作用。

【问题讨论】:

    标签: javascript jquery wrapall


    【解决方案1】:

    您可以使用.wrapAll() 来做到这一点,例如this

    jQuery('#page p').wrapAll('<div></div>');
    

    或者像this一样使用.wrapInner()

    jQuery('#page').wrapInner('<div></div>');
    

    【讨论】:

    • wrapAll("&lt;div /&gt;") 我相信也是可以接受的。如果我错了,请纠正我?
    • @Nathan - 是的,$(html) 中的任何有效内容在这里也有效,只是 IMO 的可读性稍差 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-06
    • 1970-01-01
    • 2019-11-26
    • 2010-10-26
    • 2016-05-31
    • 1970-01-01
    相关资源
    最近更新 更多