Javascript 如何从a<;中提取粗体文本;b>;使用jQuery标记
我有一些(坏的)HTML,我正试图刮,这看起来像这样Javascript 如何从a<;中提取粗体文本;b>;使用jQuery标记,javascript,jquery,cheerio,Javascript,Jquery,Cheerio,我有一些(坏的)HTML,我正试图刮,这看起来像这样 <div class="MsoNormal" style="text-align: justify;"> <span style="font-family: Georgia,"Times New Roman",serif;"> <span style="color: #c00000;">"<i style="mso-bidi-font-style: normal
<div class="MsoNormal" style="text-align: justify;">
<span style="font-family: Georgia,"Times New Roman",serif;">
<span style="color: #c00000;">"<i style="mso-bidi-font-style: normal;">Book Name</i>" by
<b style="mso-bidi-font-weight: normal;">AUTHOR</b>. Release Date:
<b style="mso-bidi-font-weight: normal;">DATE</b>. Published by
<b style="mso-bidi-font-weight: normal;">PUBLISHER</b>
</div>
只需使用$('b')
选择器:
$('b').each(function(index, element) {
console.log(element.textContent);
});
或者,如果要将它们存储在数组中,可以使用.map
方法:
var bold_words = $('b').map(function() { return this.textContent });
console.log(bold_words);
// ["AUTHOR", "DATE", "PUBLISHER"]
使用
map
函数,如下例所示get
方法将返回一个数组,然后您可以自由地对该信息执行任何操作
var text=$(“b”).map(函数(){
返回$(this.text();
}).get();
警报(文本);
警报(“粗体文本:+text.join(“”))代码>
“书名”作者
作者发行日期:
日期。出版人
出版商
“给出整个文本”。什么意思?这将为您提供每个单独的元素。什么是隐藏?不管怎样。您可以使用.text()
从元素或元素集合中获取文本。但这是初学者水平的东西,你可以在一般的教程中学习。这里有一种更为中间的方法:$(“b”).text(函数(i,txt){console.log(txt);})
…或“extract”的意思是要消除包装b
元素并保留文本?在这种情况下,$(“b”).unwrap()
。您真的需要花更多的精力来描述您的问题。@斜视为清晰起见进行了编辑,对于模糊的内容表示抱歉/。。。在这里,$('b')再次遍历所有内容
,因此在这种情况下,您需要执行:$(this)。查找('b')…
请花时间阅读初学者教程。
var bold_words = $('b').map(function() { return this.textContent });
console.log(bold_words);
// ["AUTHOR", "DATE", "PUBLISHER"]