【问题标题】:How to wrap previous and next sibling text of <br> with div using jquery?如何使用 jquery 用 div 包装 <br> 的上一个和下一个兄弟文本?
【发布时间】:2016-09-25 09:29:17
【问题描述】:

以下 HTML 结构已给出且无法更改:

<div id="cone" class="team">
  <div class="default"></div>
  ...
  <div class="default">
    <img src="img.jpg" width="271" height="271" alt="" border="0"> First Text line<br> Second text line (optional)
    <img src="img.jpg" width="271" height="271" alt="" border="0"> First Text line<br> Second text line (optional) ...
  </div>
</div>

我想使用 jQuery 得到以下结果:

<div id="cone" class="team">
  <div class="default"></div>
  ...
  <div class="default">
    <img src="img.jpg" width="271" height="271" alt="" border="0">
    <div class="desc">
      First Text line<br> Second text line (optional)
    </div>
    <img src="img.jpg" width="271" height="271" alt="" border="0">
    <div class="desc">
      First Text line<br> Second text line (optional)
    </div>
    ...
  </div>
</div>

我试过了:

$(".team img").each(function () {
    $(this.nextSibling).wrap('<div class="desc"></div>');
});

但这只包含第一行:

<img src="fileadmin/dateien/bilder/team/platzhalter_team.jpg" width="271" height="271" alt="" border="0">
<div class="desc">First Text line</div>
<br>
Second text line (optional)

重要提示:&lt;img&gt; 标签后面可以有一行、两行,甚至三行文字。

【问题讨论】:

  • 我试过 nextUntil,但它只包含 br 标签:$(this.nextSibling).nextUntil('img').wrapAll('&lt;div&gt;&lt;/div&gt;');
  • @mplungjan:抱歉,无法使用该代码...

标签: javascript jquery html nextsibling


【解决方案1】:

您需要在.default 中迭代br 元素并在循环中添加新标签。您可以使用 Node.previousSiblingNode.nextSibling 属性获取元素的上一个/下一个文本兄弟。

$(".default > br").each(function(){
    // Store previous/next sibling of br in variable then remove it.
    var prev = $(this.previousSibling).remove()[0].textContent;
    var next = $(this.nextSibling).remove()[0].textContent;
    // Insert new tag before br.
    $(this).before("<div class='desc'>" + prev + "<br>" + next + "</div>");  
}).remove(); // Remove br after loop

$(".default > br").each(function(){
    var prev = $(this.previousSibling).remove()[0].textContent;
    var next = $(this.nextSibling).remove()[0].textContent;
    $(this).before("<div class='desc'>" + prev + "<br>" + next + "</div>");
}).remove();
.desc { color: red }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="cone" class="team">    
    <div class="default"></div> 
    <div class="default">       
        <img src="img.jpg" width="271" height="271" alt="" border="0">
        First Text line<br>
        Second text line (optional)
        <img src="img.jpg" width="271" height="271" alt="" border="0">
        First Text line<br>
        Second text line (optional)
        ...
    </div>
</div>

编辑:

如果你的html有多个&lt;br&gt;,你需要将br替换为自定义文本,换行后,将目标文本替换为&lt;br&gt;。在示例中,我使用了[br]

$(".default").html(function(i, html){
    return html.replace(/<br>/g, "[br]");
});
$(".default > img").each(function(){
    $(this.nextSibling).wrap('<div class="desc"></div>');
});
$(".default").html(function(i, html){
    return html.replace(/\[br\]/g, "<br>");
});

$(".default").html(function(i, html){
    return html.replace(/<br>/g, "[br]");
});
$(".default > img").each(function(){
    $(this.nextSibling).wrap('<div class="desc"></div>');
});
$(".default").html(function(i, html){
    return html.replace(/\[br\]/g, "<br>");
});
.desc { color: red }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="cone" class="team">    
    <div class="default"></div> 
    <div class="default">       
        <img src="img.jpg" width="271" height="271" alt="" border="0">
        First Text line<br>
        Second text line (optional)<br>
        Third text line
        <img src="img.jpg" width="271" height="271" alt="" border="0">
        First Text line<br>
        Second text line (optional)<br>
        Third text line
        ...
    </div>
</div>

【讨论】:

  • 嗨,太好了,谢谢!它非常适用于用 &lt;br&gt; 分隔的 2 行,但正如最初的问题中所述:也可以有 1 行(当然没有 &lt;br&gt;)和 3 行。然后这不起作用。
【解决方案2】:

您可以将id添加到“br”,并通过id列表包装所有

<div id="id1">First</div><br id="id2">
<div id="id2">Second</div><br>
<div id="id3">First</div><br id="id4">

<script>
$("#id1,#id2,#id3,#id4").wrapAll("<div class='wraped'></div>")
</script>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-14
    • 1970-01-01
    相关资源
    最近更新 更多