jquery 从文本之间选择文本

发布于 2024-09-08 12:26:46 字数 803 浏览 3 评论 0原文

我想抓取两个标签之间的文本,该文本位于主 div 中,以下是示例...

<div class="main_result">

some text....
<div>other divs</div>
<p>some other html</p>
<div class="bread_crump"></div>

text i want to grab

<b>selected category</b>
some other text and div...

</div>

显然以下示例不起作用,但它是一个想法..

var count = $('.main_result', data)
                                .find('.bread_crump')
                                .after('<span class="count_total" style="color:red">')
                                .parents('.main_result')
                                .find('.bread_crump ~ b')
                                .before('</span>')
                                .parents('.main_result').html();

i want to grab the text between two tags, which is in main div, following is the example...

<div class="main_result">

some text....
<div>other divs</div>
<p>some other html</p>
<div class="bread_crump"></div>

text i want to grab

<b>selected category</b>
some other text and div...

</div>

obviously following example dont work, but its an idea..

var count = $('.main_result', data)
                                .find('.bread_crump')
                                .after('<span class="count_total" style="color:red">')
                                .parents('.main_result')
                                .find('.bread_crump ~ b')
                                .before('</span>')
                                .parents('.main_result').html();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

半仙 2024-09-15 12:26:46
var txt = $('.main_result')
          .contents() // get it's contents
          .filter(function() { //filter the result for textnodes that aren't empty (helps ie's parsing of textnodes)
             if( this.nodeType == 3 && $.trim(this.data) != "") { //get only text nodes and filter out whitespace elements
                 return true;
             }
             return false;
          }).get(1).data; // get 1 because it's your 2nd non-whitespace text node

var txt = $.trim(txt); // trim out all the new lines 'n stuff

不过,最好将这些元素包装在您可以a)轻松找到和b)依赖的东西中(文本节点的数量可能会变得不确定)

var txt = $('.main_result')
          .contents() // get it's contents
          .filter(function() { //filter the result for textnodes that aren't empty (helps ie's parsing of textnodes)
             if( this.nodeType == 3 && $.trim(this.data) != "") { //get only text nodes and filter out whitespace elements
                 return true;
             }
             return false;
          }).get(1).data; // get 1 because it's your 2nd non-whitespace text node

var txt = $.trim(txt); // trim out all the new lines 'n stuff

Better, though, to wrap these elements in something you can a) find easily and b) rely on (the number of text-nodes can get iffy)

听闻余生 2024-09-15 12:26:46
var firstNode = $(".main_result div.bread_crump").get(0)
var secondNode = $(".main_result b").get(0)
var nextToFirst = $("div.bread_crump").get(0).nextSibling;
if(nextToFirst.nodeType == 3 && nextToFirst.nextSibling == secondNode) {
  alert(nextToFirst.nodeValue);
}

没有太多 jQuery,但使用 DOM。

var firstNode = $(".main_result div.bread_crump").get(0)
var secondNode = $(".main_result b").get(0)
var nextToFirst = $("div.bread_crump").get(0).nextSibling;
if(nextToFirst.nodeType == 3 && nextToFirst.nextSibling == secondNode) {
  alert(nextToFirst.nodeValue);
}

Without much jQuery but using DOM.

挽你眉间 2024-09-15 12:26:46

我不知道有什么方法可以从文本块中仅选择文本的一部分,而周围没有任何标记。如果文本有任何一致的模式,我会使用

var text = $(".main_result").html();

And 然后使用 REGEX 来提取 "bread_crump" div 之后和 之前的文本

如果不将所需的文本添加到可以直接使用 jQuery 选择器引用的元素中,这将是一个肮脏的黑客行为,并且根据页面布局的一致性,页面加载之间可能会发生变化,也可能不会发生变化。如果您无法控制页面布局,那么此处提供的任何其他解决方案都将比我提供的解决方案更接近解决方案。

如果布局中有一个模式,您可以确定它是一致的,然后将其作为锚点并从那里进行选择。

I don't know of any way that you can select only a portion of text from a block of text without any markup around it. If there were any pattern to the text which is consistent, I would use

var text = $(".main_result").html();

And then use REGEX to pull the text after the "bread_crump" div and before the <b>

Without adding your desired text into an element which can referred to with a jQuery selector directly, it will be a dirty hack and depending on the consistency of the page layout, may or may not change from page load to page load. If you can't control the page layout, any of the other solutions provided here would be a closer step to a solution than what I provided.

If there's a pattern in the layout you can be sure is consistent, then focus on that as an anchor and reach your selection from there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文