jQuery實(shí)現(xiàn)搜索頁(yè)面關(guān)鍵字的功能
更新時(shí)間:2017年02月16日 08:39:09 作者:yuan10277
這篇文章主要為大家詳細(xì)介紹了jQuery實(shí)現(xiàn)搜索頁(yè)面關(guān)鍵字的功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
在一篇文章中查找關(guān)鍵字,找到后高亮顯示。

具體代碼:
<html>
<head>
<title>Search</title>
<style type="text/css">
p { border:1px solid black;width:500px;padding:5px;}
.highlight { background-color:yellow; }
</style>
</head>
<body>
<form>
<p>
I consider that a man's brain originally is like a little empty attic, and you have to stock it with such furniture as you choose. A fool takes in all the lumber of every sort that he comes across, so that the knowledge which might be useful to him gets crowded out, or at best is jumbled up with a lot of other things, so that he has a difficulty in laying his hands upon it.
</p>
<p>
I consider that a man's brain originally is like a little empty attic, and you have to stock it with such furniture as you choose. A fool takes in all the lumber of every sort that he comes across, so that the knowledge which might be useful to him gets crowded out, or at best is jumbled up with a lot of other things, so that he has a difficulty in laying his hands upon it.
</p>
<p>
I consider that a man's brain originally is like a little empty attic, and you have to stock it with such furniture as you choose. A fool takes in all the lumber of every sort that he comes across, so that the knowledge which might be useful to him gets crowded out, or at best is jumbled up with a lot of other things, so that he has a difficulty in laying his hands upon it.
</p>
<input type="text" id="text"/>
<input type="button" id="search" value="Search"/>
<input type="button" id="clear" value="Clear"/>
</form>
<script type="text/javascript" src="../jquery.js"></script>
<script type="text/javascript">
$(document).ready(function ()
{
$('#search').click(highlight);//點(diǎn)擊search時(shí),執(zhí)行highlight函數(shù);
$('#clear').click(clearSelection);//點(diǎn)擊clear按鈕時(shí),執(zhí)行clearSelection函數(shù);
function highlight()
{
clearSelection();//先清空一下上次高亮顯示的內(nèi)容;
var searchText = $('#text').val();//獲取你輸入的關(guān)鍵字;
var regExp = new RegExp(searchText, 'g');//創(chuàng)建正則表達(dá)式,g表示全局的,如果不用g,則查找到第一個(gè)就不會(huì)繼續(xù)向下查找了;
$('p').each(function()//遍歷文章;
{
var html = $(this).html();
var newHtml = html.replace(regExp, '<span class="highlight">'+searchText+'</span>');//將找到的關(guān)鍵字替換,加上highlight屬性;
$(this).html(newHtml);//更新文章;
});
}
function clearSelection()
{
$('p').each(function()//遍歷
{
$(this).find('.highlight').each(function()//找到所有highlight屬性的元素;
{
$(this).replaceWith($(this).html());//將他們的屬性去掉;
});
});
}
});
</script>
</body>
</html>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- JQuery+JS實(shí)現(xiàn)仿百度搜索結(jié)果中關(guān)鍵字變色效果
- jquery+json實(shí)現(xiàn)的搜索加分頁(yè)效果
- 基于jquery的頁(yè)面劃詞搜索JS
- jquery中用jsonp實(shí)現(xiàn)搜索框功能
- jQuery Jsonp跨域模擬搜索引擎
- jquery 表格排序、實(shí)時(shí)搜索表格內(nèi)容(附圖)
- jQuery插件select2利用ajax高效查詢大數(shù)據(jù)列表(可搜索、可分頁(yè))
- jQuery UI仿淘寶搜索下拉列表功能
- jQuery下拉美化搜索表單效果代碼分享
- 基于jQuery實(shí)現(xiàn)頁(yè)面搜索功能
- JavaScript前端頁(yè)面搜索功能案例【基于jQuery】
相關(guān)文章
jquery中l(wèi)oad方法的用法及注意事項(xiàng)說(shuō)明
本篇文章主要是對(duì)jquery中l(wèi)oad方法的用法及注意事項(xiàng)進(jìn)行了詳細(xì)介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2014-02-02
jquery的冒泡事件的阻止與允許(三種實(shí)現(xiàn)方法)
冒泡或默認(rèn)的事件發(fā)生,在某些時(shí)候是不需要的,在此就需要一些可以阻止冒泡和默認(rèn)的事件的方法,本文介紹三種方法做到不同程度的阻止,感興趣的朋友可以了解下,或許對(duì)你了解冒泡事件有所幫助2013-02-02
jquery 回車(chē)事件實(shí)現(xiàn)代碼
簡(jiǎn)單地記下jquery實(shí)現(xiàn)回車(chē)事件,學(xué)習(xí)jquery的朋友可以參考下。2011-08-08
jqGrid日期格式的判斷示例代碼(開(kāi)始日期與結(jié)束日期)
jqGrid日期格式的判斷示例代碼(開(kāi)始日期與結(jié)束日期)。需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2013-11-11
jsTree 基于JQuery的排序節(jié)點(diǎn) Bug
jsTree(JQuery) 排序節(jié)點(diǎn) Bug的解決方法。2011-07-07
使用ajaxfileupload.js實(shí)現(xiàn)上傳文件功能
這篇文章主要為大家詳細(xì)介紹了使用ajaxfileupload.js實(shí)現(xiàn)上傳文件功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08
jQuery實(shí)現(xiàn)web頁(yè)面櫻花墜落的特效
這篇文章主要介紹了jQuery實(shí)現(xiàn)web頁(yè)面櫻花墜落的特效,效果非常棒,需要的朋友可以參考下2017-06-06

