jquery插件方式實(shí)現(xiàn)table查詢功能的簡(jiǎn)單實(shí)例
1. 寫(xiě)插件部分,如下:
;(function($){ $.fn.plugin = function(options){ var defaults = { //各種屬性,各種參數(shù) } var options = $.extend(defaults, options); this.each(function(){ //功能代碼 var _this = this; }); } })(jQuery);
附上一個(gè)例子:
;(function($){ $.fn.table = function(options){ var defaults = { //arguments , properties evenRowClass : 'evenRow', oddRowClass : 'oddRow', currentRowClass : 'currentRow', eventType : 'mouseover', eventType2 : 'mouseout', } var options = $.extend(defaults, options); this.each(function(){ //function code var _this = $(this); //even row _this.find('tr:even:not("#thead")').addClass(options.evenRowClass); //_this.find('#thead').removeClass(options.evenRowClass); // odd row _this.find('tr:odd').addClass(options.oddRowClass); /*_this.find('tr').mouseover(function(){ $(this).addClass(options.currentRowClass); }).mouseout(function(){ $(this).removeClass(options.currentRowClass); });*/ _this.find('tr').bind(options.eventType, function(){ $(this).addClass(options.currentRowClass); }); _this.find('tr').bind(options.eventType2, function(){ $(this).removeClass(options.currentRowClass); }); }); return this; } })(jQuery);
html部分調(diào)用插件如下:
();== ();==(function(){});==$(document).ready();
等頁(yè)面加載成功后執(zhí)行
;$(function(){ $('#table1').table({ //arguments , properties evenRowClass : 'evenRow1', oddRowClass : 'oddRow1', currentRowClass : 'currentRow1' }); });
附上代碼:
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="Generator" content="EditPlus®"> <meta name="Author" content=""> <meta name="Keywords" content=""> <meta name="Description" content=""> <title>Document</title> <style> *{margin:0; padding:0;} table{ border-collapse:collapse; width:100%; border:1px solid red; margin-top:50px; text-align:center; } tr, th, td{ height:30px; border:1px solid red; } .evenRow1{ background:red; } .oddRow1{ background:orange; } .currentRow1{ background:blue; } #ss{ float:right; margin-right:100px; } #search{ font-size:14px; width:50px; } </style> <script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script> <script src="jquery-table-1.0.js"></script> </head> <body> <script> ;$(function(){ $('#table1').table({ //arguments , properties evenRowClass : 'evenRow1', oddRowClass : 'oddRow1', currentRowClass : 'currentRow1' }); $('input[type=button]').click(function(){ var text = $('input[type=text]').val(); $('#table1 tr:not("#thead")').hide().filter(':contains("'+text+'")').show(); }); }); </script> <div id="ss"> <input type="text" placeholder="請(qǐng)輸入查詢數(shù)據(jù)"> <input id="search" type="button" value="查詢"> </div> <table id="table1"> <tr id="thead"> <th>姓名</th> <th>學(xué)號(hào)</th> <th>性別</th> <th>年齡</th> </tr> <tr> <td>張三</td> <td>1</td> <td>男</td> <td>20</td> </tr> <tr> <td>李四</td> <td>2</td> <td>男</td> <td>30</td> </tr> <tr> <td>張三</td> <td>1</td> <td>女</td> <td>20</td> </tr> <tr> <td>李四</td> <td>2</td> <td>男</td> <td>30</td> </tr> <tr> <td>王五</td> <td>3</td> <td>男</td> <td>30</td> </tr> <tr> <td>王五</td> <td>3</td> <td>男</td> <td>30</td> </tr> <tr> <td>張三</td> <td>1</td> <td>女</td> <td>20</td> </tr> <tr> <td>李四</td> <td>2</td> <td>男</td> <td>30</td> </tr> </table> </body> </html>
通過(guò)這個(gè)例子學(xué)到了jquery 對(duì)象級(jí)插件開(kāi)發(fā)
以上這篇jquery插件方式實(shí)現(xiàn)table查詢功能的簡(jiǎn)單實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
jquery select動(dòng)態(tài)加載選擇(兼容各種瀏覽器)
jquery select動(dòng)態(tài)加載選擇,兼容各種瀏覽器包括ie6,在ie6下會(huì)報(bào)錯(cuò),不過(guò)我們已有解決方法,感興趣的朋友可以了解下,或許對(duì)你學(xué)習(xí)jquery有所幫助2013-02-02jquery 簡(jiǎn)短幾句代碼實(shí)現(xiàn)給元素動(dòng)態(tài)添加及獲取提示信息
雖然是很基本的東西,但為什么很基本的方法就可以實(shí)現(xiàn)的東西有些人偏偏還要去追求復(fù)雜高深難懂的呢?這里只是交流而已2011-09-09jquery UI Datepicker時(shí)間控件的使用方法(基礎(chǔ)版)
這篇文章主要介紹了jquery ui datepicker時(shí)間控件的使用方法,需要的朋友可以參考下2015-11-11jQuery實(shí)現(xiàn)手機(jī)自定義彈出輸入框
這篇文章主要介紹了jQuery實(shí)現(xiàn)手機(jī)自定義彈出輸入框 的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06jquery ajax的success回調(diào)函數(shù)中實(shí)現(xiàn)按鈕置灰倒計(jì)時(shí)
實(shí)現(xiàn)異步手機(jī)發(fā)送短信成功之后在ajax的success回調(diào)中實(shí)現(xiàn)將發(fā)送按鈕置灰并倒計(jì)時(shí),具體的實(shí)現(xiàn)示例如下,喜歡的朋友可以研究下2013-11-11jQuery實(shí)現(xiàn)鍵盤(pán)回車(chē)搜索功能
本文通過(guò)代碼給大家介紹了jquery實(shí)現(xiàn)鍵盤(pán)回車(chē)搜索功能,前臺(tái)代碼和搜索按鈕實(shí)現(xiàn)代碼都給大家貼出了,需要的朋友參考下吧2017-07-07jquery無(wú)法為動(dòng)態(tài)生成的元素添加點(diǎn)擊事件的解決方法(推薦)
下面小編就為大家?guī)?lái)一篇jquery無(wú)法為動(dòng)態(tài)生成的元素添加點(diǎn)擊事件的解決方法(推薦)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧,祝大家游戲愉快哦2016-12-12jquery和雅虎的yql服務(wù)實(shí)現(xiàn)天氣預(yù)報(bào)服務(wù)示例
本文介紹一個(gè)利用Jquery和雅虎的YQL服務(wù)實(shí)現(xiàn)天氣預(yù)報(bào)功能,需要的朋友可以參考下2014-02-02