简体   繁体   English

包装多个 <p> 在jQuery的div中

[英]wrap multiple <p> in a div with jQuery

In the following markup there are two <p> tags. 在以下标记中,有两个<p>标记。 How can I wrap both of them in a single div using jQuery? 如何使用jQuery将它们都包装在一个div中?

<div class="box">
    <ul>
        <li>1</li>
        <li>1</li>
    </ul>
    <p>paragraph 1</p>
    <p>paragraph 2</p>
</div>

Desired output should be: 所需的输出应为:

<div class="wrap">
    <p>paragraph 1</p>
    <p>paragraph 2</p>
</div>

I have been trying jQuery function .wrap, but failed to do so. 我一直在尝试jQuery函数.wrap,但是没有这样做。 I could only try following but it would wrap both <p> in separate <div> : 我只能尝试跟随,但是它将两个<p>分别包装在单独的<div>

$( "p" ).wrap( "<div class='wrap'></div>" );

JSFiddle: http://jsfiddle.net/avfr8fpe/ JSFiddle: http : //jsfiddle.net/avfr8fpe/

You can use wrapAll : 您可以使用wrapAll

$( "p" ).wrapAll( "<div class='wrap'></div>" );

Updated jsFiddle 更新了jsFiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM