jQuery 语法错误:函数语句需要名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16477563/
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
SyntaxError: function statement requires a name
提问by JohnDoo
im confused about this error which i become without todo any event like click etc. Yes i used the search here, but in all of the cases i found the situation was another problem.
我对这个错误感到困惑,我没有做任何事件,如点击等。是的,我在这里使用了搜索,但在所有情况下,我发现情况是另一个问题。
The "SyntaxError: function statement requires a name" was throwing by firebug after a pagerefresh here: onGoTo:function(evt){
“语法错误:函数语句需要一个名称”在页面刷新后被萤火虫抛出: onGoTo:function(evt){
this is my function:
这是我的功能:
function exampleFc(ctx){
'use strict';
var goTo = $('a[href*=#]',this.$ctx);
goTo.on('click',$.proxy(this.onGoTo,this));
onGoTo:function(evt){
evt.preventDefault();
var elem = $(evt.currentTarget).attr('href');
if(elem.length > 1){
$('html,body').animate({
scrollTop: $(elem).offset().top
}, 700, function (){location.hash = elem;});
}
return false;
}
};
};
whats wrong? i want to call the function onGoTo by catching the click event.
怎么了?我想通过捕捉点击事件来调用 onGoTo 函数。
回答by Prakash GPz
the syntax is incorrect. that syntax should be used only inside objects.
语法不正确。该语法应仅在对象内部使用。
var obj = { onGoTo: function(){ } }
in areas other than object you should use this syntax:
在对象以外的区域中,您应该使用以下语法:
var onGoTo = function( ) { }; // function declaration
onGoto() // function call
or, in case of your code,
或者,如果是您的代码,
this.onGoTo = function() {}; // function declaration
this.onGoTo(); // function call
finally your code should be like this:
最后你的代码应该是这样的:
function exampleFc(ctx){
'use strict';
var goTo = $('a[href*=#]',this.$ctx);
goTo.on('click',$.proxy(this.onGoTo,this));
this.onGoTo = function(evt) {
evt.preventDefault();
var elem = $(evt.currentTarget).attr('href');
if(elem.length > 1){
$('html,body').animate({
scrollTop: $(elem).offset().top
}, 700, function (){location.hash = elem;});
}
return false;
}
}
回答by JohnDoo
ok, as Prakash GPz already says is the syntax wrong.
好的,正如 Prakash GPz 已经说过的那样语法错误。
My solution to solved it is:
我解决它的解决方案是:
function exampleFc(ctx){
'use strict';
var onGoTo = function(evt){
evt.preventDefault();
var elem = $(evt.currentTarget).attr('href');
if(elem.length > 1){
$('html,body').animate({
scrollTop: $(elem).offset().top
}, 700, function (){location.hash = elem;});
}
return false;
};
var goTo = $('a[href*=#]',ctx);
goTo.on('click',onGoTo));
};
};
by the way, the function above you can use to scroll anywhere in the DOM. make sure that the scroll link have a href attribute starts with the #-Symbol and that this id exists.
顺便说一下,您可以使用上面的函数在 DOM 中的任何位置滚动。确保滚动链接具有以 #-Symbol 开头的 href 属性,并且此 ID 存在。
best
最好的事物
回答by Mohammad Reza Farahani
This line cause your error
此行导致您的错误
onGoTo:function(evt){
Because inside functions you can't use object syntax and have to use =
instead of :
因为在函数内部,您不能使用对象语法,而必须使用=
代替:
var onGoTo = function(evt){
Note that proxy object is a part of es6 implementation.
请注意,代理对象是 es6 实现的一部分。