书签 分享 收藏 举报 版权申诉 / 13

类型jQuery插件开发全解析.docx

  • 文档编号:12596405
  • 上传时间:2023-04-20
  • 格式:DOCX
  • 页数:13
  • 大小:20.48KB
     

5.

 'orange' }">     

6.  Have a nice day!

     

7.

     

8.

 'green' }">     

9.  Have a nice day!

     

10.

     

11.现在我们能高亮哪些div仅使用一行脚本:

   

12.$('.hilight').hilight();     

2.7整合

下面使我们的例子完成后的代码:

Java代码

1.// 创建一个闭包     

2.(function($) {     

3.  // 插件的定义     

4.  $.fn.hilight = function(options) {     

5.    debug(this);     

6.    // build main options before element iteration     

7.    var opts = $.extend({}, $.fn.hilight.defaults, options);     

8.    // iterate and reformat each matched element     

9.    return this.each(function() {     

10.      $this = $(this);     

11.      // build element specific options     

12.      var o = $.meta ?

 $.extend({}, opts, $this.data()) :

 opts;     

13.      // update element styles     

14.      $this.css({     

15.        backgroundColor:

 o.background,     

16.        color:

 o.foreground     

17.      });     

18.      var markup = $this.html();     

19.      // call our format function     

20.      markup = $.fn.hilight.format(markup);     

21.      $this.html(markup);     

22.    });     

23.  };     

24.  // 私有函数:

debugging     

25.  function debug($obj) {     

26.    if (window.console && window.console.log)     

27.      window.console.log('hilight selection count:

 ' + $obj.size());     

28.  };     

29.  // 定义暴露format函数     

30.  $.fn.hilight.format = function(txt) {     

31.    return '' + txt + '';     

32.  };     

33.  // 插件的defaults     

34.  $.fn.hilight.defaults = {     

35.    foreground:

 'red',     

36.    background:

 'yellow'     

37.  };     

38.// 闭包结束     

39.})(jQuery);      

这段设计已经让我创建了强大符合规范的插件。

我希望它能让你也能做到。

3、总结

jQuery为开发插件提拱了两个方法,分别是:

jQuery.fn.extend(object);给jQuery对象添加方法。

jQuery.extend(object);为扩展jQuery类本身.为类添加新的方法。

3.1jQuery.fn.extend(object);

fn是什么东西呢。

查看jQuery代码,就不难发现。

jQuery.fn=jQuery.prototype={

init:

function(selector,context){//....

//......

};

原来jQuery.fn=jQuery.prototype.对prototype肯定不会陌生啦。

虽然javascript 没有明确的类的概念,但是用类来理解它,会更方便。

jQuery便是一个封装得非常好的类,比如我们用语句 $("#btn1")会生成一个jQuery类的实例。

jQuery.fn.extend(object);对jQuery.prototype进得扩展,就是为jQuery类添加“成员函数”。

jQuery类的实例可以使用这个“成员函数”。

比如我们要开发一个插件,做一个特殊的编辑框,当它被点击时,便alert当前编辑框里的内容。

可以这么做:

$.fn.extend({

alertWhileClick:

function(){

$(this).click(function(){

alert($(this).val());

});

}

});

$("#input1").alertWhileClick();//页面上为:

$("#input1") 为一个jQuery实例,当它调用成员方

配套讲稿:

如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。

特殊限制:

部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。

关 键  词:
jQuery 插件 开发 解析
提示  冰豆网所有资源均是用户自行上传分享,仅供网友学习交流,未经上传用户书面授权,请勿作他用。
关于本文
本文标题:jQuery插件开发全解析.docx
链接地址:https://www.bdocx.com/doc/12596405.html
关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

copyright@ 2008-2022 冰点文档网站版权所有

经营许可证编号:鄂ICP备2022015515号-1

收起
展开