Javascript 从jquery插件外部调用此.each(function(){})内的函数
如何从脚本外部访问“alertDate()”函数 如果我使用:Javascript 从jquery插件外部调用此.each(function(){})内的函数,javascript,jquery,Javascript,Jquery,如何从脚本外部访问“alertDate()”函数 如果我使用: (function( $ ){ $.fn.foo = function(params) { params = $.extend( { on: false }, params); this.each(function(){ if(params.on) { function alertDate(){ alert('FOO
(function( $ ){
$.fn.foo = function(params) {
params = $.extend( {
on: false
}, params);
this.each(function(){
if(params.on) {
function alertDate(){
alert('FOO BAR!!!');
}
}
});
};
})( jQuery );
将给我访问函数的权限,好的,一切都很好,我将在这个.each(function(){})内部获得对函数“alertDate()”的访问权限
我想通过以下方式从外部访问函数“alertDate()”:
$('#test').foo()
我该怎么做
提前感谢,并为糟糕的英语感到抱歉您可以这样做,但这不是插件通常的工作方式
$('#text').foo({on: 'true'}).each().alertDate();
下面是我将如何处理它,以便您的
alertDate()
函数可以在内部和外部使用:
$.fn.foo = function() {
function bar() {
alert("bar");
}
this.bar = bar;
return this;
};
$("a").foo().bar();
.fn.foo??有没有输入错误?这不是jQuery插件的正常设计模式-你能解释一下你想要实现什么吗?@Alnitak,我有一个650多行的代码,我只是重新创建了我的问题,以便有人理解我想要访问什么。我想从
this.each(function(){})
waaaaaaa访问函数。嗯,这有很多事情要做。在代码按照预期的方式工作之前,有很多事情需要更改,并且在尝试调用函数的方式中,有许多约定正在被打破。如果你想在聊天室里慢慢讨论这个问题,我会很高兴,但我不知道怎么做:(有人知道怎么做吗?xD@Áxel,我的代码也可以。正如我所说的。我没有写650多行不工作的代码。我已经遵循了jquery官方文档中关于插件创建的内容,我的插件工作是一种魅力。我在问题中发布的示例,我不得不(再次)说),是我想解决的问题的一个示例。我想访问此中的一个函数。每个(function(){})
。无论如何,感谢您的观察。谢谢!解决了我的问题!
$.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();