javascript 如何调用jQuery嵌套函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18974744/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to call the jQuery nested function
提问by Suresh Pattu
I am trying to call the nested function is not working
Here is what I tried jsfiddle
我试图调用嵌套函数不起作用
这是我尝试过的jsfiddle
Script:
脚本:
(function( $ ){
$.fn.investPage = function() {
function setupFCConfig(){
$('.nestedFunction').click(function(){
alert('setupFCConfig func()');
});
}
$(".edit").on('click', function(){
alert('edit click func()');
});
$(".cancel").on('click', function(){
alert('cancel click func()');
});
$(".checkout").click(function(){
alert('checkout click func()');
});
};
})( jQuery );
$.fn.investPage();
$.fn.investPage.setupFCConfig();
回答by jfriend00
setupFCConfig()
is NOT a property of the $.fn.investPage object so it can't be called like this:
setupFCConfig()
不是 $.fn.investPage 对象的属性,因此不能像这样调用:
$.fn.investPage.setupFCConfig();
It is a local function that is not available outside the scope in which it is declared. If you want it available from an outside scope, then you need to assign it to be a property of some object that is available in that outside scope.
它是一个局部函数,在它声明的范围之外不可用。如果您希望它从外部作用域可用,那么您需要将它分配为在该外部作用域中可用的某个对象的属性。
For example, you could change the definition of that function to be like this:
例如,您可以将该函数的定义更改为如下所示:
(function( $ ){
$.fn.investPage = function() {
$(".edit").on('click', function(){
alert('edit click func()');
});
$(".cancel").on('click', function(){
alert('cancel click func()');
});
$(".checkout").click(function(){
alert('checkout click func()');
});
};
$.fn.investPage.setupFCConfig = function (){
$('.nestedFunction').click(function(){
alert('setupFCConfig func()');
});
}
})( jQuery );
$.fn.investPage();
$.fn.investPage.setupFCConfig();
FYI, you also need to fix the misspelling of .click
.
仅供参考,您还需要修复.click
.
回答by Patrick Evans
you are using the wrong scope for the function
您为函数使用了错误的作用域
(function( $ ){
$.fn.investPage = function() {
$(".edit").on('click', function(){
alert('edit click func()');
});
$(".cancel").on('click', function(){
alert('cancel click func()');
});
$(".checkout").click(function(){
alert('checkout click func()');
});
};
$.fn.investPage.setupFCConfig = function(){
$('.nestedFunction').click(function(){
alert('setupFCConfig func()');
});
};
})( jQuery );
or
或者
(function( $ ){
$.fn.investPage = function() {
this.setupFCConfig = function(){
$('.nestedFunction').click(function(){
alert('setupFCConfig func()');
});
};
$(".edit").on('click', function(){
alert('edit click func()');
});
$(".cancel").on('click', function(){
alert('cancel click func()');
});
$(".checkout").click(function(){
alert('checkout click func()');
});
return this;
};
})( jQuery );
var page = $.fn.investPage();
page.setupFCConfig();
The second returns the investPage
object where you can than access the function from the object variable.
第二个返回investPage
可以从对象变量访问函数的对象。
回答by Martin
Use it this way jsFiddle updatedIn the scope of $.fn.investPage
the this
is not the $.fn.investPage
object. So your object does not knowthe function setupFCConfig()
.
使用这种方式的jsfiddle更新的能力范围$.fn.investPage
的this
不是$.fn.investPage
对象。所以你的对象不知道这个函数setupFCConfig()
。
But you can use:
但是你可以使用:
$.fn.investPage.setupFCConfig = function(){
$('.nestedFunction').click(function(){
alert('setupFCConfig func()');
});
};
to achive your goal.
实现你的目标。
回答by Martin
you can make an object and use it for namespace like below too
您也可以创建一个对象并将其用于命名空间,如下所示
investPage = {
init: function () {
$(".edit").on('click', function(){
alert('edit click func()');
});
$(".cancel").on('click', function(){
alert('cancel click func()');
});
$(".checkout").click(function(){
alert('checkout click func()');
});
},
setupFCConfig: function (){
$('.nestedFunction').click(function(){
alert('setupFCConfig func()');
});
}
}
investPage.init();
investPage.setupFCConfig();
回答by Gurminder Singh
Change the code to :
将代码更改为:
(function( $ ){
$.fn.investPage = function() {
this.setupFCConfig: function (){
$('.nestedFunction').click(function(){
alert('setupFCConfig func()');
});
};
$(".edit").on('click', function(){
alert('edit click func()');
});
$(".cancel").on('click', function(){
alert('cancel click func()');
});
$(".checkout").click(function(){
alert('checkout click func()');
});
};
})( jQuery );
var instance = $.fn.investPage();
instance.setupFCConfig();