【发布时间】:2013-02-15 12:23:58
【问题描述】:
如下面的 sn-p 所示,我有多个文本 div,其中有一个粗体部分,然后是一个换行符,然后是一段文本。我可以找到()粗体部分,但我怎样才能使用 javascript/jquery 仅获取粗体部分之后的换行符之后的文本部分?
<div class="thecontent">
any amount of text or html elements before
<b>
the bolded text
</b>
<br>
the text I need together with the bolded text which can contain other html
elements apart from line breaks and bolded blocks
<br>
<b>
posibility of more bolded and text couples further in the div
</b>
<br>
and some more text to go with the bolded text
</div>
在一个 div 中可以有多个粗体部分和文本对,并且需要的文本片段以换行符结尾、另一个粗体部分或 div 的结尾。文本块中可能还有其他html元素,如<a href>。
我可以使用.find('b') 获取<b> </b> 的内容,并且我尝试使用nodeType == 3 来选择文本节点,但这只能获取所有文本。
很遗憾,我无法更改页面的 html。有没有人有解决方案?在此先感谢:)
根据要求,输入将以粗体形式阻止换行符和后面的文本。我需要文本跟随它们直到换行或另一个粗体部分。
输出将是一个变量中的粗体文本以及换行符之后但直到另一个变量中的下一个换行符或粗体元素的文本。
所以 html 示例的输出是:the bolded text + the text I need together with the bolded text which can contain other html
elements apart from line breaks and bolded blocks
和posibility of more bolded and text couples further in the div + and some more text to go with the bolded text
【问题讨论】:
-
我无法理解您的问题,您想要获得非粗体(在
<b> </b>内)但仍在div.thecontent内的文本内容?跨度> -
我的目标是将粗体部分与其后面的文本配对(直到下一个换行符或下一个粗体部分)
-
你为什么不把你的div分成更小的容器(比如
<div>或<span>),每个容器都包含你想要过滤的连贯文本部分? -
@Joe 你能让你的问题简单易懂吗?
-
@Joe,你能创建一个简单的实际输入和预期输出示例吗?
标签: javascript jquery html web-scraping