(function( $ ){
$.fn.foo = function(params) {
params = $.extend( {
on: false
}, params);
this.each(function(){
if(params.on) {
function alertDate(){
alert('FOO BAR!!!');
}
}
});
};
})( jQuery );
如何从脚本外访问“alertDate()”函数?
如果我使用:
$('#test').foo()
将允许我访问该函数,好的,一切正常,我将在 this.each(function(){}) 内部访问函数“alertDate()”。
我想通过类似的方式从外部访问函数“alertDate()”:
$('#text').foo({on: 'true'}).each().alertDate();
我该怎么做?
提前致谢,抱歉英语不好
最佳答案
下面是我将如何处理它,以便您的 alertDate()
函数可以在内部和外部使用:
$.fn.foo = function(params) {
params = $.extend( {
on: false
}, params);
this.alertDate = function(){
alert("FOO BAR!!!");
}
this.each(function(){
if(params.on) {
this.alertDate();
}
});
return this;
};
//Externally the alertDate function could be called as:
$("test").foo().alertDate();
关于javascript - 从 jquery 插件外部调用 this.each(function(){}) 中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14259234/