Why does jsfiddle throw error that function is not

2019-01-15 20:12发布

This question already has an answer here:

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:

error ReferenceError: AddNewData is not defined {"error": "Please use POST request"}

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?

1条回答
来,给爷笑一个
2楼-- · 2019-01-15 20:20

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.

Change it to No wrap - in <head>

http://jsfiddle.net/62Ls6x9n/160/

查看更多
登录 后发表回答