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 执行


基础教程推荐
- jQuery File Upload - 如何识别所有文件何时上传 2022-01-01
- 如何在特定日期之前获取消息? 2022-01-01
- Javascript 在多个元素上单击事件侦听器并获取目标 2022-01-01
- 什么是不使用 jQuery 的经验技术原因? 2022-01-01
- 为什么我在 Vue.js 中得到 ERR_CONNECTION_TIMED_OUT? 2022-01-01
- Node.js 有没有好的索引/搜索引擎? 2022-01-01
- 每次设置弹出窗口的焦点 2022-01-01
- WatchKit 支持 html 吗?有没有像 UIWebview 这样的控制器? 2022-01-01
- 如何使用sencha Touch2在单页中显示列表和其他标签 2022-01-01
- 如何使用 CSS 显示和隐藏 div? 2022-01-01