2017-09-26 52 views
0

无法使其正常工作,因此适用于每个帖子。围绕除第1段以外的所有段落划分

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标签。

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

我想在一个div中包括多个段落排除第一个。我需要在第一段后插入,然后在最后一段之后关闭。

+0

您不能插入部分或未关闭的标签。 –

+0

你是什么意思“我不想要,因为我需要使用最后孩子添加它”?您是否试图在“span”中包装一个或多个元素?如果是这样,请更新您的问题。 – fubar

+0

你看看'.wrap' for jquery – zgood

回答

1

在这里,你去了一个解决方案https://jsfiddle.net/htuczx5q/3/

$('p').not('p:first').wrap('<span class="text" />')
<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>

我用jQuery wrap。它将包裹除first paragraph以外的所有paragraph

要定位的特定讯息(段落),那么它可以通过三种方式来完成:

的ClassName
$('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" />');

希望这会帮助你。

+0

帮助是,但它会影响所有帖子。我需要代码来影响每个单独的条目,而不会影响档案中的所有帖子。 – Dev

+0

@Dev然后我会建议,通过'classname'或'id'或'data-attribute'来定位文章。让我更新会帮助你的答案。 – Shiladitya

+0

给你一个大25,但它仍然不适用于包含多个帖子的档案,因为它会影响所有帖子。 – Dev