Function doesn#39;t execute from an onclick inside the html(函数不会从 html 内的 onclick 执行)
问题描述
这可能很简单,但我对 JavaScript 很陌生,这让我发疯了.
this is probably stupidly easy but I'm very new to JavaScript and it's driving me nuts.
我的问题,为什么这不起作用:http://jsfiddle.net/Ye9tG/
My question, why doesn't this work: http://jsfiddle.net/Ye9tG/
<input type="button" id="butt" value="Button" onclick="getThought();"/>
如果我将 onclick 直接添加到 JavaScript 中,效果很好:
It works fine if I add the onclick directly into the JavaScript:
document.getElementById("butt").onclick = getThought;
提前致谢.
推荐答案
您的 getThoughts 函数未定义,因为您的 JavaScript 设置为执行 onLoad.查看 jsFiddle 左上角的下拉菜单.选择No wrap - in <head>"解决问题:http://jsfiddle.net/Ye9tG/1/
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/
另外,请务必查看浏览器的控制台以检查错误.在这种情况下,您会在单击按钮时看到 Uncaught ReferenceError: getThought is not defined 错误.
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 执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:函数不会从 html 内的 onclick 执行
基础教程推荐
- 即使用户允许,Gmail 也会隐藏外部电子邮件图片 2022-01-01
- 在 contenteditable 中精确拖放 2022-01-01
- Fabric JS绘制具有活动形状的多边形 2022-01-01
- 原生拖动事件后如何获取 mouseup 事件? 2022-01-01
- 检查 HTML5 拖放文件类型 2022-01-01
- 如何添加到目前为止的天数? 2022-01-01
- Bootstrap 模态出现在背景下 2022-01-01
- fetch 是否支持原生多文件上传? 2022-01-01
- Bokeh Div文本对齐 2022-01-01
- npm start 错误与 create-react-app 2022-01-01
