:如何使用jQuery突出显示某些单词 [英] : How to highlight certain words with jQuery
本文介绍了:如何使用jQuery突出显示某些单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在寻找一个脚本,该脚本可以根据其位置突出显示一定数量的单词. 例如,对于以下内容,我只想突出显示第二,第三和第四个单词:
I'm looking for a script than can highlight a certain number of words depending on their position. example, for the following contentI want to highlight only the second, third and fourth words:
<p>
Quisque bibendum sem ut lacus. Integer dolor ullamcorper libero.
Aliquam rhoncus eros at augue. Suspendisse vitae mauris.
</p>
结果应该像:
<p>
Quisque <span class="highlight">bibendum sem ut</span> lacus. Integer dolor ullamcorper libero.
Aliquam rhoncus eros at augue. Suspendisse vitae mauris.
</p>
非常感谢您!
推荐答案
此jQuery插件是您要寻找的:
This jQuery plugin is what you're looking for:
它完全按照您的要求进行操作,遍历DOM TextNodes并查找要搜索的文本,当发现一个出现时,它会创建一个span元素.
It does exactly what you want, traverses the DOM TextNodes and looks for the text to search, when it find one occurrence it creates an span element.
用法:
$('p').highlight('bibendum sem ut');
这篇关于:如何使用jQuery突出显示某些单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文