国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

jQuery實現(xiàn)搜索頁面關(guān)鍵字的功能

2019-11-19 17:33:35
字體:
供稿:網(wǎng)友

在一篇文章中查找關(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);//點擊search時,執(zhí)行highlight函數(shù);     $('#clear').click(clearSelection);//點擊clear按鈕時,執(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,則查找到第一個就不會繼續(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> 

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 天全县| 阿克陶县| 信丰县| 巨野县| 湘潭县| 康平县| 海门市| 嵩明县| 临海市| 玉树县| 泸溪县| 弥渡县| 陈巴尔虎旗| 祁连县| 特克斯县| 松桃| 望城县| 砀山县| 临江市| 图片| 汉寿县| 嫩江县| 曲松县| 平远县| 仁化县| 黔江区| 铜川市| 新绛县| 托克托县| 绩溪县| 双鸭山市| 泸州市| 汉源县| 岑溪市| 砀山县| 宝鸡市| 南开区| 阜新市| 汝南县| 晴隆县| 宜良县|