作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
例如,我正在查看 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/
我是一名优秀的程序员,十分优秀!