了解jQuery插件开发模式

编程入门 行业动态 更新时间:2024-10-27 18:20:56
本文介绍了了解jQuery插件开发模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

根据 jQuery插件创作指南,基本的插件结构如下所示:

According to the jQuery plugin authoring guidelines, a basic plugin structure would look like this:

(function($){ $.fn.myPlugin = function( options ) { return this.each(function() { // Do something... }); }; })( jQuery );

然而,我在我检查的几个jQuery插件中看到了以下模式:

Yet I've seen the following pattern in several jQuery plugins I've inspected:

(function($){ $.extend($.fn, { myPlugin: function( options ) { $(this).each( function() { // Do something }); }, }) })(jQuery);

有人可以解释第二种方法 - 什么是 $ .extend(.. 。以及所有关于对象的符号?

Can someone explain the second approach- What is $.extend(... and the object-notation all about?

谢谢 -

推荐答案

这两个基本上完成了相同的任务,但方式略有不同。

These two are basically accomplishing the same task, but in a slightly different manner.

$。fn.myPlugin = ... 直接将函数分配给jQuery名称空间中所需的位置。

$.fn.myPlugin = … is directly assigning the function to the place you want it in the jQuery namespace.

$ .extend( $ .fn,{myPlugin:... 扩展 $。fn 对象,对象指定为第二个参数在这种情况下,对象只包含一个属性 myPlugin ,这是要添加的函数。

$.extend($.fn, {myPlugin:… is extending the $.fn object with the object specified as the second parameter. In this case, the object simply contains a single property, myPlugin, which is the function to add.

你可以使用任何一种结构,虽然我个人觉得第一种结构更清洁。

You can use either structure, although I personally find the first a bit cleaner.

更多推荐

了解jQuery插件开发模式

本文发布于:2023-07-23 09:31:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1195976.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:插件   模式   jQuery

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!