Javascript 为什么这个简单的 JSFiddle 不起作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7043649/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 00:35:02  来源:igfitidea点击:

Why does this simple JSFiddle not work?

javascripthtmljsfiddle

提问by thomasf1

There is some code I wanted to put into JSFiddle. It didn't work. Narrowing it down I can't even get this simplest of code to work:

我想将一些代码放入 JSFiddle。它没有用。缩小范围我什至无法让这个最简单的代码工作:

JSFiddle

JSFiddle

function displaymessage() {
  alert("Hello World!");
}
<form>
  <input type="button" value="Click me!" onclick="displaymessage()" />
</form>

<p>By pressing the button above, a function will be called. The function will alert a message.</p>

The alertbox doesn't show up in the JSFiddle.

alert框未显示在 JSFiddle 中。

采纳答案by Praveen Prasad

http://jsfiddle.net/praveen_prasad/XNJxT/14/

http://jsfiddle.net/praveen_prasad/XNJxT/14/

Js fiddle so something like this to whatever you write

Js 对你写的任何东西都像这样

window.addEvent('load',function(){

//Your code


});

enter image description here

在此处输入图片说明

回答by Igor Dymov

Select No wrap - bottom of <head>in the “Load type” dropdown in the JavaScript settings.

No wrap - bottom of <head>在 JavaScript 设置的“加载类型”下拉列表中选择。

Screenshot of the dropdown mentioned above

上面提到的下拉菜单的屏幕截图

回答by John Kalberer

You need to take your function out of the onLoad/onReadyotherwise it is placed inside of another scope and your button cannot access the function. In your case you need to use No wrap (head)

您需要将您的函数从 onLoad/onReady 中取出,否则它会被放置在另一个范围内并且您的按钮无法访问该函数。在您的情况下,您需要使用 No wrap (head)

The code generated looks like this:

生成的代码如下所示:

Ext.onReady(function() {
    function displaymessage()
    {
        alert("Hello World!");
    }
});

回答by FishBasketGordo

Change the code to run "no wrap (head)" instead of "onDomReady". Your function isn't visible to your markup as is.

更改代码以运行“no wrap (head)”而不是“onDomReady”。您的函数对您的标记不可见。

回答by Shahin Mammadzada

Maybe some of you need to move your function outside of document.readyif you use html onclick="func()"

如果您使用 html onclick="func()",也许有些人需要将您的功能移到document.ready之外

$(document).ready(function() {
  ...
}

function func(){
  ...
}

回答by Korreca

Sorry but... is much simpler than what you propose...

对不起,但是......比你提出的要简单得多......

If you want to call a js function with "onclick" attribute, put your javascrit code directly in head, you dont need domready or similars.

如果你想调用一个带有“onclick”属性的js函数,直接把你的javascrit代码放在head里,你不需要domready或类似的东西。

Second, you need to include "javascript:" before the function name.

其次,您需要在函数名称之前包含“javascript:”。

See: http://jsfiddle.net/XNJxT/1838/

见:http: //jsfiddle.net/XNJxT/1838/

<input type="button" value="Click me!" onclick="javascript:displaymessage()" />

回答by zNiine

My problem to this was when I made my script tag I typed it out :

我的问题是当我制作脚本标签时,我输入了它:

script type="javascript/text"

instead of :

代替 :

script type="text/javascript"

and now it works.

现在它起作用了。