javascript 未捕获的 ReferenceError: 'functionName' 未定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21993604/
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
Uncaught ReferenceError: 'functionName' not defined
提问by Arty
So I have checked my script tags in my .jsp file:
所以我检查了我的 .jsp 文件中的脚本标签:
<script type="text/javascript" src="javascript/jquery-1.3.1.min.js"></script>
<script language="JavaScript" > "some content here ...." </script>
and below in the same .jsp file I have a tag:
在同一个 .jsp 文件中,我有一个标签:
<body BGCOLOR="white" text="black" link="blue" vlink="red" onLoad="functionName();enableBackButton();">
However, in my JavaScript file I have:
但是,在我的 JavaScript 文件中,我有:
$(document).ready(function(){
$('current').click(function(event){
function functionName() { ....... }
Somehow I keep getting an error in my Chrome console stating:
不知何故,我在 Chrome 控制台中不断收到错误消息:
Uncaught ReferenceError: functionName is not defined
未捕获的 ReferenceError:未定义 functionName
回答by Felix
Move your functionName()
out side of $(document).ready(function(){
移动你的functionName()
外侧$(document).ready(function(){
function functionName() { ....... }
$(document).ready(function(){
$('.current').click(function(event){
functionName();
});
});
Also, you need to use .
to target element by class or #
to target element by id
此外,您需要使用.
按类#
定位元素或按id
So $('.current')
will select any element with class="current"
and $('#current')
will select an element with id="current"
因此,$('.current')
将选择与任何元素class="current"
和$('#current')
将选择一个元素id="current"
Last note is to update your jQuery version since 1.3.1
is extremely outdated already and it lacks of many helpful and important features which is supported by later versions.
最后要注意的是更新您的 jQuery 版本,因为1.3.1
它已经非常过时了,并且缺少许多后续版本支持的有用且重要的功能。
回答by Tushar Gupta - curioustushar
Don't wrap your function in DOM ready($(document).ready(function(){)
handler.As it is a anonymous function so your functionName()
function has local scope
.So you can not call it outside the DOM ready
不要将你的函数包装在处理程序中。DOM ready($(document).ready(function(){)
因为它是一个匿名函数,所以你的functionName()
函数有local scope
.所以你不能在外面调用它DOM ready
Read What is the scope of variables in JavaScript?
回答by Rafa Hernández
When you call functionName in onLoad, the function is not yet in the document. Try to put the function functionName out. For instance,
当您在 onLoad 中调用 functionName 时,该函数尚未出现在文档中。尝试把函数functionName 去掉。例如,
<script language="JavaScript" >
"some content here ...."
function functionName() {
your function
}
</script>
回答by Pointy
Moving the function would be one way to fix it, but an alternative is to simply call the function from your "ready" handler, or if it really matters that all page content is loaded, from a "load" handler:
移动函数将是修复它的一种方法,但另一种方法是简单地从“就绪”处理程序调用该函数,或者如果加载所有页面内容真的很重要,则从“加载”处理程序调用:
$(document).ready(function() {
function functionName() {
// whatever
}
functionName();
});
Polluting the global namespace unnecessarily is generally a bad thing.
不必要地污染全局命名空间通常是一件坏事。
回答by Suman Bogati
function functionName() {
should defined outside the $(document).ready(function(){
, like
function functionName() {
应该在 之外定义$(document).ready(function(){
,比如
function functionName() {...}
$(document).ready(function(){...});
If you defined it inside the document.ready
then it's scope would limit into this function.
如果你在里面定义了它,document.ready
那么它的范围将限制在这个函数中。
Only you can defined when the functionName()
would call from inside $(document).ready(
like
只有你可以定义时functionName()
会从内部调用$(document).ready(
像
$(document).ready(function(){
function functionName() {...}
// but in your case below function
// is calling from body when its loaded
functionName(); //from here you can call
}
);