函数不会从html内的onclick中执行 [英] Function doesn't execute from an onclick inside the html

查看:385
本文介绍了函数不会从html内的onclick中执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能非常容易,但我对JavaScript很陌生,这让我感到非常紧张。

我的问题,为什么不能这样工作:
http://jsfiddle.net/Ye9tG/

 < input type =buttonid = buttvalue =Buttononclick =getThought();/> 

如果直接将onclick添加到JavaScript中,它可以正常工作:

  document.getElementById(butt)。onclick = getThought; 

预先致谢。 您的 getThoughts 函数未定义,因为您的JavaScript是设置为执行 onLoad 。请参阅jsFiddle左上角的下拉菜单。选择无包装< head> 来解决问题: http://jsfiddle.net/Ye9tG/1/



另外,请始终查看浏览器的控制台以检查错误。在这种情况下,您会看到一个 Uncaught ReferenceError:单击该按钮时,getThought未定义错误。


this is probably stupidly easy but I'm very new to JavaScript and it's driving me nuts.

My question, why doesn't this work: http://jsfiddle.net/Ye9tG/

<input type="button" id="butt" value="Button" onclick="getThought();"/>

It works fine if I add the onclick directly into the JavaScript:

document.getElementById("butt").onclick = getThought;

Thanks in advance.

解决方案

Your getThoughts function isn't defined, because your JavaScript is set to execute onLoad. See the dropdown menu in the upper left of jsFiddle. Select "No wrap - in <head>" to resolve the issue: http://jsfiddle.net/Ye9tG/1/

Also, always take a look at your browser's console to check for errors. In this case, you'll see a Uncaught ReferenceError: getThought is not defined error when clicking the button.

这篇关于函数不会从html内的onclick中执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆