除第1段外,包括所有段落的除法

时间:2017-09-26 01:26:31

标签: jquery html css insertafter

无法使其正常工作,因此适用于每个帖子。

https://jsfiddle.net/haymanpl/htuczx5q/

<p>This is an example of a WordPress post, you could edit this to put information about yourself or your site so readers know where you are coming from. You can create as many posts as you like in order to share with your readers what is on your mind.</p>

<div class="text">

<p>This is an example of a WordPress post, you could edit this to put information about yourself or your site so readers know where you are coming from. You can create as many posts as you like in order to share with your readers what is on your mind.</p>

这是一个WordPress帖子的示例,您可以对其进行编辑以显示有关您自己或您网站的信息,以便读者了解您的来源。您可以根据自己的喜好创建任意数量的帖子,以便与读者分享您的想法。

 </div>

$( "p:first-child" ).after(function() {

    return '<div class="text">';

    });

这有效,但它添加了我不想要的结束div标签,因为我需要使用last-child添加它。

   $('p:first-child').after( $('<div class="text">') );

我正在尝试在div中包含多个段落,不包括第一个段落。我需要在第一段之后插入,然后在最后一段之后插入。

1 个答案:

答案 0 :(得分:1)

您可以使用解决方案https://jsfiddle.net/htuczx5q/3/

&#13;
&#13;
$('p').not('p:first').wrap('<span class="text" />')
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>This is an example of a WordPress post, you could edit this to put information about yourself or your site so readers know where you are coming from. You can create as many posts as you like in order to share with your readers what is on your mind.</p>


<p>This is an example of a WordPress post, you could edit this to put information about yourself or your site so readers know where you are coming from. You can create as many posts as you like in order to share with your readers what is on your mind.</p>
&#13;
&#13;
&#13;

我使用了jQuery wrap。除了paragraph之外,它将包装所有first paragraph

要定位特定帖子(段落),可以通过三种方式完成:

<强>类名
$('p.className').not('p:first').wrap('<span class="text" />');

<强> ID
$('p#id').not('p:first').wrap('<span class="text" />');

数据属性
$('p[data-attr="post1"]').not('p:first').wrap('<span class="text" />');

希望这会对你有所帮助。