zoukankan      html  css  js  c++  java
  • jquery插件开发总结

    jquery的插件开发主要分三种:$.extend(), $.fn,$.widget()应用jQuery UI的部件工厂方式创建

    通常使用第二种方式进行开发。

    第一种方式如下: 

    $.extend({
      sayHello: function(name) {
        console.log('Hello,' + (name ? name : 'Dude') + '!');
      }
    })
    $.sayHello(); //调用
    $.sayHello('Wayou'); //带参调用

    但这种方式无法利用jQuery强大的选择器带来的便利,要处理DOM元素以及将插件更好地运用于所选择的元素身上,还是需要使用第二种开发方式。

    第二种开发方式如下:

    比如我们将页面上所有链接颜色转成红色,则可以这样写这个插件:

    $.fn.myPlugin = function() {
      //在这里面,this指的是用jQuery选中的元素
      //example :$('a'),则this=$('a')
      this.css('color', 'red');
    }

    jQuery一个时常优雅的特性是支持链式调用,选择好DOM元素后可以不断地调用其他方法,要让插件不打破这种链式调用,只需return一下即可。

    $.fn.myPlugin = function() {
      //在这里面,this指的是用jQuery选中的元素
      this.css('color', 'red');
      return this.each(function() {
        //对每个元素进行操作
        $(this).append(' ' + $(this).attr('href'));
      }))
    }

    需要传参的时候。。

    $.fn.myPlugin = function(options) {
      var defaults = {
        'color': 'red',
        'fontSize': '12px'
      };
      var settings = $.extend(defaults, options);
      return this.css({
        'color': settings.color,
        'fontSize': settings.fontSize
      });
    }

    上面代码调用extend时会将defaults的值改变,好的做法是将一个新的空对象做为$.extend的第一个参数,defaults和用户传递的参数对象紧随其后,这样做的好处是所有值被合并到这个空对象上,保护了插件里面的默认值。

    $.fn.myPlugin = function(options) {
      var defaults = {
        'color': 'red',
        'fontSize': '12px'
      };
      var settings = $.extend({},defaults, options);//将一个空对象做为第一个参数
      return this.css({
        'color': settings.color,
        'fontSize': settings.fontSize
      });
    }

    用面向对象的编程思想更好对代码进行维护

    //定义Beautifier的构造函数
    var Beautifier = function(ele, opt) {
      this.$element = ele,
      this.defaults = {
        'color': 'red',
        'fontSize': '12px',
        'textDecoration':'none'
      },
      this.options = $.extend({}, this.defaults, opt)
    }
    //定义Beautifier的方法
    Beautifier.prototype = {
      beautify: function() {
        return this.$element.css({
          'color': this.options.color,
          'fontSize': this.options.fontSize,
          'textDecoration': this.options.textDecoration
        });
      }
    }
    //在插件中使用Beautifier对象
    $.fn.myPlugin = function(options) {
      //创建Beautifier的实体
      var beautifier = new Beautifier(this, options);
      //调用其方法
      return beautifier.beautify();
    }

    为了避免污染全局变量,我们可以使用自调用匿名函数包裹代码:

    (function() {
      //定义Beautifier的构造函数
      var Beautifier = function(ele, opt) {
        this.$element = ele,
        this.defaults = {
          'color': 'red',
          'fontSize': '12px',
          'textDecoration': 'none'
        },
        this.options = $.extend({}, this.defaults, opt)
      }
      //定义Beautifier的方法
      Beautifier.prototype = {
        beautify: function() {
          return this.$element.css({
            'color': this.options.color,
            'fontSize': this.options.fontSize,
            'textDecoration': this.options.textDecoration
          });
        }
      }
      //在插件中使用Beautifier对象
      $.fn.myPlugin = function(options) {
        //创建Beautifier的实体
        var beautifier = new Beautifier(this, options);
        //调用其方法
        return beautifier.beautify();
      }
    })();

    在代码的开头加上;以避免和别的代码产生冲突,同时,将系统变量以参数形式传递到插件内部。

    ;(function($,window,document,undefined){
      //todo
    })(jQuery,window,document);
  • 相关阅读:
    Spring Boot (20) 拦截器
    Spring Boot (19) servlet、filter、listener
    Spring Boot (18) @Async异步
    Spring Boot (17) 发送邮件
    Spring Boot (16) logback和access日志
    Spring Boot (15) pom.xml设置
    Spring Boot (14) 数据源配置原理
    Spring Boot (13) druid监控
    Spring boot (12) tomcat jdbc连接池
    Spring Boot (11) mybatis 关联映射
  • 原文地址:https://www.cnblogs.com/doudoujun/p/6402248.html
Copyright © 2011-2022 走看看