jquery 从文本之间选择文本
我想抓取两个标签之间的文本,该文本位于主 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不过,最好将这些元素包装在您可以a)轻松找到和b)依赖的东西中(文本节点的数量可能会变得不确定)
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)
没有太多 jQuery,但使用 DOM。
Without much jQuery but using DOM.
我不知道有什么方法可以从文本块中仅选择文本的一部分,而周围没有任何标记。如果文本有任何一致的模式,我会使用
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
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.