我想基于类型文本的输入和突出显示文本做一个简单的jQuery单词搜索。我怎样才能做到这一点多个词?此代码仅适用于单个字符。jquery简单的搜索建议文本突出显示
的Html
<input type="text"/>
<div>John Resig
George Martin
Malcom John Sinclair
J. Ohn
Sample text
</div>
CSS
span {
background : red
}
JS
$("input").keyup(function() {
var mysearchword = $("input:text").val();
var word = mysearchword;
$("div:contains('"+word+"')").html(function(i,html) {
var re = new RegExp(word,"g");
return html.replace(re,'<span>'+word+'</span>')
});
});
这个答案对你有帮助吗?http://stackoverflow.com/questions/8644428/how-to-highlight-text-using-javascript – Tony
本教程也看起来很有帮助:http://www.the -art-of-web.com/javascript/search-highlight/ – Tony
尝试阅读此:http://stackoverflow.com/questions/7571117/jquery-something-like-contains-but-to-match-exactly-phrase – Teo