如何从<b>使用jQuery标记
How to extract bold text from a <b> tag using jQuery
我有一些(坏的)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;">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>
我需要用粗体提取三件事,即AUTHOR、DATE&出版商
我试过$('strong,b').each(...)
之类的东西,但它给出了整个文本。
编辑:这是我正在使用的部分代码,基本上我想做的是从一组这样的div中获取所有细节。
$(".MsoNormal").each(function(index) {
var book = {}
var elem = $(this).text()
elem = sanitizeString(elem) // Removes whitespaces and line breaks
book["title"] = getTitle(elem) // Gets the book name, which is between double quotes
//Get author,date & publisher here $('b') traverses everything again
}
})
使用map
函数,如下例所示。get
方法将返回一个数组,然后您可以自由地对该信息执行任何操作。
var text = $("b").map(function() {
return $(this).text();
}).get();
alert(text);
alert("Bold text: " + text.join(" "));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<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"]
相关文章:
- 如何使用jQuery自动打开页面上的所有链接
- 如何使用jquery在填充自动完成的值后使文本框只读
- 使用 jQuery 的 .on 函数如何获取事件的原始元素
- 如何在Angular2中使用jQuery插件
- 如何使用jQuery选择下拉列表的值
- 使用JQuery解析JSON嵌套数组
- 在不使用JQuery的情况下隐藏DOM中的选定元素
- 如何使用jquery处理php循环通过元素
- 使用jquery将mysql数据获取到新的表行中
- 如何使用jquery Validation验证Formspread
- 是否有任何方法可以使用jQuery替换在数组中定义值的文本
- 使用jQuery动态添加表并在其中动态添加行
- 使用jquery在单击时在单元格中输入值
- 如何使用jQuery在动画中期加速动画
- 如何使用jquery强制一个单词更改大小写等以保留品牌
- 在不使用jquery的情况下查找页面中的所有锚点并附加函数
- 使用JQuery的动态上下文菜单
- 使用jQuery以红色和黑色闪烁文本
- 如何使用jquery更改html中的背景颜色
- 使用jQuery从原始页面内容创建iframe