【问题标题】:Wrap Div Around All Paragraphs Except 1st Paragraph除第 1 段外的所有段落环绕 Div
【发布时间】:2017-09-26 01:26:30
【问题描述】:

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

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 中,不包括第一个。我需要在第一段之后插入,然后在最后一段之后插入结尾。

【问题讨论】:

  • 您不能插入部分或未闭合的标签。
  • 你是什么意思“我不想要因为我需要使用 last-child 添加它”?您是否尝试将一个或多个元素包装在 span 中?如果是这样,请更新您的问题。
  • 你看看 .wrap 的 jquery
  • 您的标题询问了 div,但您的代码提到了跨度。是哪一个?
  • span 是段落内元素,因此您不应将 p 包装在 span 标签内。 (w3.org/TR/html5/dom.html#phrasing-content-1)。您可以通过将它们包装在 div 中来做您需要的事情。你需要做什么?

标签: jquery html css insertafter


【解决方案1】:

这里有一个解决方案https://jsfiddle.net/htuczx5q/3/

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

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

类名
$('p.className').not('p:first').wrap('&lt;span class="text" /&gt;');

ID
$('p#id').not('p:first').wrap('&lt;span class="text" /&gt;');

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

希望这会对你有所帮助。

【讨论】:

  • 有帮助,但它会影响所有帖子。我需要代码来影响每个条目,而不影响存档中的所有帖子。
  • @Dev 然后我会建议,将帖子定位为classnameiddata-attribute。让我更新对您有帮助的答案。
  • 给了你大 25,但它仍然不适用于包含多个帖子的存档,因为它会影响所有帖子。
  • @Dev 最后一个问题,您将如何确定要使用 div 包装的帖子?你有任何类或数据属性?
  • 我可以使用 .entry 或 .post。
猜你喜欢
  • 2018-05-30
  • 2016-12-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多