本文實例講述了JavaScript前端頁面搜索功能。分享給大家供大家參考,具體如下:
今天給大家分享一篇關于前端頁面搜索的案例,有了這個案例,在表格數(shù)據(jù)中可以進行快速查找,比在瀏覽器中使用ctrl+F體驗比較好。
效果圖:
代碼:
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>頁面搜索實例</title> <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script> <style> table{ width:400px; border:1px solid blue; border-collapse: collapse; } table th{ height:30px; border:1px solid blue; text-align: center; } table td{ height:30px; border:1px solid blue; text-align: center; } </style></head><body> <table> <tr> <th>姓名</th> <th>性別</th> <th>電話</th> </tr> <tr> <td>張三</td> <td>男</td> <td>13111112222</td> </tr> <tr> <td>李四</td> <td>男</td> <td>13233334444</td> </tr> <tr> <td>移動充值</td> <td>女</td> <td>10086</td> </tr> <tr> <td>聯(lián)通充值</td> <td>女</td> <td>10010</td> </tr> </table> <div style="width:100%;height:20px"></div> <div> <input type="text" name="" id=""> <input type="button" value="搜索"> </div></body><script> $('input[type=button]').click(function(){ var text = $('input[type=text]').val(); $('table tr').not(':first').hide().filter(':contains("'+text+'")').show(); });</script></html>
代碼比較簡單,首先給button按鈕添加單擊事件,然后獲取文本框中的內(nèi)容,再從表格中tr進行查找,首先把表頭的tr過濾掉,然后把其他的tr全部隱藏掉,然后按照內(nèi)容進行過濾,把過濾出來的行顯示出來。
感興趣的朋友可以使用在線HTML/CSS/JavaScript代碼運行工具:http://tools.VeVB.COm/code/HtmlJsRun測試上述代碼運行效果。
注:另外,本站如下幾款在線工具也具有前端頁面搜索功能,且功能更加強大:
php在線函數(shù)參考表:
http://tools.VeVB.COm/table/php_fun_table
全國少數(shù)民族分布在線查詢工具:
http://tools.VeVB.COm/bianmin/minzufenbu
世界節(jié)日在線查詢工具:
http://tools.VeVB.COm/bianmin/jieri
世界各國區(qū)號代碼及時差查詢表:
http://tools.VeVB.COm/bianmin/shicha
世界各國/地區(qū)貨幣查詢表:
http://tools.VeVB.COm/bianmin/huobi
世界各國首都查詢表:
http://tools.VeVB.COm/bianmin/shoudu
更多關于jQuery相關內(nèi)容可查看本站專題:《jQuery頁面元素操作技巧匯總》、《jQuery常見事件用法與技巧總結》、《jQuery常用插件及用法總結》、《jQuery擴展技巧總結》及《jquery選擇器用法總結》
希望本文所述對大家jQuery程序設計有所幫助。
新聞熱點
疑難解答