gpt4 book ai didi

jQuery 插件创作 : Why do some do jQuery. pluginName 和其他 jQuery.fn.pluginName?

转载 作者:行者123 更新时间:2023-12-03 23:00:25 25 4
gpt4 key购买 nike

例如,我正在查看 jCalendar 源代码,创建者有该插件的两个不同部分,一个是“jQuery.jcalendar”下的函数,另一个是“jQuery.fn.jcalendar”下的函数。两人分开的目的是什么?其中一个比另一个做什么?

最佳答案

jQuery.fn.mypluging 名称扩展了 jQuery 对象:

$(selector); //a jquery object
$(selector).myplugin();

jQuery.myplugin 扩展了 jquery 对象本身:

$; //the jQuery object
$.myPlugin();

通过将插件添加到 jQuery.fn,您可以对该选择器找到的对象执行一些操作:

jQuery.fn.makeRed = function(){
this.each( function() {
$(this).css('color', 'red');
}
}

$('div.someClass').makeRed(); //makes all divs of class someclass have red text

扩展 jQuery 对象本身通常是为了类需要但不扩展 jQuery 对象的函数。因此,扩展我们之前的示例:

jQuery.fn.doStuff = function(){
this.each( function() {
$(this).css('color', 'red')
.append($.doStuff.giveMeRandom());
}
}

jQuery.doStuff = {
giveMeRandom: function() {
return Math.random();
}
}

$('div.someClass').doStuff(); //makes all divs of class someclass have red text and append a random number to them

关于jQuery 插件创作 : Why do some do jQuery. pluginName 和其他 jQuery.fn.pluginName?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/538043/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com