jquery插件方式实现table查询功能的简单实例


Posted in Javascript onJune 06, 2016

1. 写插件部分,如下:

;(function($){

$.fn.plugin = function(options){



var defaults = {




//各种属性,各种参数



}



var options = $.extend(defaults, options);

 

this.each(function(){




//功能代码




var _this = this;



});


}

})(jQuery);

附上一个例子:

;(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部分调用插件如下:

();== ();==(function(){});==$(document).ready();

等页面加载成功后执行

;$(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="请输入查询数据">
 <input id="search" type="button" value="查询">
 </div>

 <table id="table1">
  <tr id="thead">
    <th>姓名</th>
    <th>学号</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>

通过这个例子学到了jquery 对象级插件开发

以上这篇jquery插件方式实现table查询功能的简单实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
Firefox 无法获取cssRules 的解决办法
Oct 11 Javascript
一个很简单的jquery+xml+ajax的无刷新树结构(无css,后台是c#)
Jun 02 Javascript
了不起的node.js读书笔记之node.js中的特性
Dec 22 Javascript
Backbone.js的一些使用技巧
Jul 01 Javascript
Bootstrap实现弹性搜索框
Jul 11 Javascript
用headjs来管理和加载js 提高网站加载速度
Nov 29 Javascript
vue使用混入定义全局变量、函数、筛选器的实例代码
Jul 29 Javascript
快速对接payjq的个人微信支付接口过程解析
Aug 15 Javascript
微信小程序实现左侧滑动导航栏
Apr 08 Javascript
taro 实现购物车逻辑的实例代码
Jun 05 Javascript
JS继承实现方法及优缺点详解
Sep 02 Javascript
Axios取消重复请求的方法实例详解
Jun 15 Javascript
全面解析Bootstrap中tab(选项卡)的使用方法
Jun 06 #Javascript
全面解析Bootstrap中scrollspy(滚动监听)的使用方法
Jun 06 #Javascript
JavaScript 数组中最大最小值
Jun 05 #Javascript
使用three.js 画渐变的直线
Jun 05 #Javascript
jquery判断input值不为空的方法
Jun 05 #Javascript
jQuery四种选择器使用及示例
Jun 05 #Javascript
JavaScript和jquery获取父级元素、子级元素、兄弟元素的方法
Jun 05 #Javascript
You might like
Terran剧情介绍
2020/03/14 星际争霸
php摘要生成函数(无乱码)
2012/02/04 PHP
PHP实现提取一个图像文件并在浏览器上显示的代码
2012/10/06 PHP
thinkphp的c方法使用示例
2014/02/24 PHP
PHP常用工具类大全附全部代码下载
2015/12/07 PHP
Avengerls vs Newbee BO3 第一场2.18
2021/03/10 DOTA
javascript之bind使用介绍
2011/10/09 Javascript
js获取 type=radio 值的方法
2014/05/09 Javascript
jQuery中focus事件用法实例
2014/12/26 Javascript
JavaScript中的eval()函数使用介绍
2014/12/31 Javascript
Jquery数字上下滚动动态切换插件
2015/08/08 Javascript
JavaScript常用基础知识强化学习
2015/12/09 Javascript
javascript产生随机数方法汇总
2016/01/25 Javascript
Bootstrap教程JS插件滚动监听学习笔记分享
2016/05/18 Javascript
jQuery自定义插件详解及实例代码
2016/12/29 Javascript
AngularJS中$http的交互问题
2017/03/29 Javascript
Angular4学习之Angular CLI的安装与使用教程
2018/01/04 Javascript
微信小程序实现侧边栏分类
2019/10/21 Javascript
JavaScript获取当前url路径过程解析
2019/12/27 Javascript
python读取TXT每行,并存到LIST中的方法
2018/10/26 Python
Python解析、提取url关键字的实例详解
2018/12/17 Python
pycharm 将python文件打包为exe格式的方法
2019/01/16 Python
python 列表中[ ]中冒号‘:’的作用
2019/04/30 Python
Python+pyplot绘制带文本标注的柱状图方法
2019/07/08 Python
Numpy对数组的操作:创建、变形(升降维等)、计算、取值、复制、分割、合并
2019/08/28 Python
Pycharm如何自动生成头文件注释
2020/11/14 Python
有关打架的检讨书
2014/01/25 职场文书
社区网格化管理实施方案
2014/03/21 职场文书
家长会主持词
2014/03/26 职场文书
文化建设工作方案
2014/05/12 职场文书
群众路线领导班子四风对照检查材料
2014/09/27 职场文书
党员个人对照检查材料
2014/10/01 职场文书
部门经理助理岗位职责
2015/04/13 职场文书
承诺书模板大全
2015/05/04 职场文书
Apache Pulsar集群搭建部署详细过程
2022/02/12 Servers
「女孩的钓鱼慢活」全新版权绘公布
2022/03/21 日漫