javascript - jQuery:如何在两个闭合的 html 标签之间选择文本

标签 javascript jquery html

我正在尝试使用 jQuery 将介绍/帮助文本包装在 html 文档中。它不在任何标签内,而是在两个闭合的 html 标签之间。

例如,请参阅附加的代码片段。第二个结束标签也可以不是 <p> .

var txtHelp = jQuery('b.page-title').nextUntil('p').text();
console.log(txtHelp);

//jQuery('b.page-title').nextUntil('p').text().wrap("<p />");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<b class="page-title"><h4>System Log</h4><hr class="text-primary"></b>
How to select this text and wrap it in new P-Tag
<p align="center">This can by any html tag</p>

最佳答案

nextUntil() 方法不选择文本节点。

可以通过 nextSibling获取文本节点节点的属性,通过textContent获取文本内容 文本节点的属性。

var txtHelp = jQuery('b.page-title')[0] // get the dom object
  .nextSibling // get the text node next to it
  .textContent; // get text content
console.log(txtHelp);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<b class="page-title"><h4>System Log</h4><hr class="text-primary"></b>
How to select this text and wrap it in new P-Tag
<p align="center">This can by any html tag</p>


更新 1: 如果您想用 p 标签包装元素,那么就这样做吧。

$( // wrap by jquery to convert to jQuery object
  $('b.page-title')[0] // get the dom element also you can use `get(0)`
  .nextSibling // get the textnode which is next to it
).wrap('<p/>'); //  wrap the element by p tag
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<b class="page-title"><h4>System Log</h4><hr class="text-primary"></b>
How to select this text and wrap it in new P-Tag
<p align="center">This can by any html tag</p>


更新 2: 如果它包含 br 标签并且您想将其作为文本包含,那么使用 contents() 做一些棘手的事情 方法。

var get = false;

$($('b.page-title')
  .parent() // get it's parent
  .contents() // get all children node including text node
  .filter(function() {
    if ($(this).is('b.page-title')) {
      get = true; // if element is 'b.page-title' then set flag true , we need to get element from here
      return false // return false that we don't need the 'b.page-title'
    }
    if ($(this).is('p')) // check element is `p`, that we need to get element uptop tag
      get = false; // update flag
    return get; // return the flag for filtering
  })).wrapAll('<p/>'); // use wrapAll to wrap all elements withing single tag
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<b class="page-title"><h4>System Log</h4><hr class="text-primary"></b>
How to select this text
<br/>and wrap it in new P-Tag
<p align="center">This can by any html tag</p>

关于javascript - jQuery:如何在两个闭合的 html 标签之间选择文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38450881/

相关文章:

jquery根据图像大小设置div容器的高度和宽度

javascript - 删除在 div 外部单击的功能

html - 如何仅垂直定位绝对div?

javascript - 是否可以为每个页面设置SSG和SSR?

javascript - jQuery 在特定时间内突出显示行

javascript - 动态边界更改不会在 'react-leaflet' 中产生任何影响

javascript - 单击按钮时的 LocalStorage

javascript - 在 php 中检索 formdata .append 方法

javascript - Rails respond_to 究竟是如何工作的?

html - 如何在 Bootstrap 中将长列表句子分成多行