使用javascript jQuery突出显示html中的文本 [英] highlight text in html with javascript jQuery
本文介绍了使用javascript jQuery突出显示html中的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果关键字是
pre>
var keywords =hello,thanks,goodbye//这应该是一个数组
< p> hello world< / p>
你好应该用蓝色突出显示
解决方案
我认为您正在寻找 jQuery highlight 插件。
加载后,你可以这样做:
var words =你好,谢谢,再见;
var keywords = words.split(',');
for(var x = 0; x< keywords.length; x ++){
$(selector).highlight($ .trim(keywords [x]));
}
其中选择器
是您要查找的文档中的哪个元素。如果你希望它完成整个页面,只需输入'body'
。
I want to highlight all the keywords (case insensitive) within a p tag programatically
if the keywords are
var keywords = "hello,thanks, goodbye" // this should be an array
<p>hello world</p>
hello should be highlighted in blue
解决方案
I think you are looking for the jQuery highlight plugin.
Once you have it loaded, you can just do something like this:
var words = "hello,thanks, goodbye";
var keywords = words.split(',');
for(var x = 0; x < keywords.length; x++) {
$(selector).highlight($.trim(keywords[x]));
}
Where selector
is what element in the document you want to look for. If you want it to be done to the entire page, just put 'body'
.
这篇关于使用javascript jQuery突出显示html中的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文