我想基於類型文本的輸入和突出顯示文本做一個簡單的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