为什么jsfiddle抛出错误,函数未定义? [英] Why does jsfiddle throw error that function is not defined?
问题描述
我测试的例子非常简单:只需一个按钮,当点击按钮时,将调用一个函数。但是,我得到:
The example I was testing is very simple: just one button, and when button is clicked then a function will be called. However, I'm getting:
错误ReferenceError:AddNewData未定义
{error:请使用POST请求}
error ReferenceError: AddNewData is not defined {"error": "Please use POST request"}
这是jsfiddle链接: http://jsfiddle.net/tridip/62Ls6x9n/158/
Here is the jsfiddle link: http://jsfiddle.net/tridip/62Ls6x9n/158/
<button onClick="javascript:AddNewData();return false;">Add New Data</button>
function AddNewData()
{
alert("test");
}
有人可以告诉我那里有什么问题吗?
Can someone tell me what is wrong there?
推荐答案
这是因为默认情况下,jsfiddle使用 onLoad
代码的JS定义。
It's because by default jsfiddle uses onLoad
JS definition for your code.
就是这样 - 它包含在一个匿名函数中,因此所有符号(变量,函数等)都在其范围内。
That's it - it's wrapped in an anonymous function so all symbols (variables, functions, etc) are scoped within.
将其更改为没有换行< head>
http://jsfiddle.net/62Ls6x9n/160/
这篇关于为什么jsfiddle抛出错误,函数未定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!