问题描述
我有一个查询,因为我想准备一个JSFiddle。但它不适用于小型点击程序。其中只有两行代码。
HTML
<身体GT;
< button onclick =javascript:launchdialog();>点击我< /按钮>
< / body>
JavaScript
函数launchdialog(){
alert('test');
}
我在两行代码中没有发现任何错误。
请看这个 -
您可以在head或body中设置 No wrap 选项。
当您使用 onload 或 onDomReady ,您的代码被封装在另一个函数中,该函数在 load 或准备好事件。因此,您的功能不能从该功能以外访问。而且你会得到错误
lockquote
ReferenceError:functionName没有定义
使函数无包装使它成为全局,可以从任何地方访问。
I have one query, for that I wanted to prepare a JSFiddle. But it's not working for a small click program. Which has just two lines of code.
HTML
<body> <button onclick="javascript:launchdialog();">Click Me</button> </body>
JavaScript
function launchdialog() { alert('test'); }
I didn't find anything wrong in just two lines of code.Please have a look at this JSFiddle - http://jsfiddle.net/g47qqzra/
You can set the option to No wrap in head or body.
When you use onload or onDomReady, your code is wrapped inside another function that is invoked on load or ready event. So, your function is not accessible from outside of that function. And you'll get error
Making the function no wrap makes it global, can be accessed from anywhere.
这篇关于JsFiddle中的代码不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!