我的 .cshtml 文件中有一些 html/javascript。
当我尝试将它移到 jsfiddle 中进行试验时,它不起作用。
不确定是不是我缺乏 javascript 经验,jsfiddle 经验,可能两者都有……
HTML:
<div>
<button name="startBtn" id="startBtn" onclick="startTimer">Start</button>
</div>
(我也为 onclick 属性尝试了“startTimer()”;结果相同。)
javascript:
function startTimer() { alert("startTimer"); }
当我点击按钮时,我在控制台中看到了这个:
Uncaught ReferenceError: startTimer is not defined
我忽略了什么?
(jsfiddle:http://bit.ly/1buQx9t)
最佳答案
jsFiddle Demo
首先您必须将框架设置为No wrap - in <head>
, 而不是 onLoad
.
描述
onLoad
与window.onload=function(){[YOU ARE TYPING HERE]}
相同在 JavaScript 或 $(function(){[YOU ARE TYPING HERE]});
在 jQuery 中。
No wrap - in <head>
与<head><script type="text/javascript">[YOU ARE TYPING HERE]</script</head>
相同
No wrap - in <body>
与<body>[HTML CODE HERE]<script type="text/javascript">[YOU ARE TYPING HERE]</script></head>
相同
我希望这可以清除 jsFiddle 中的设置, 它们可能有些令人困惑。
第二你的HTML
有点不对:
HTML
<div>
<input type="button' value='"Start" onclick="startTimer()"/>
</div>
描述
onclick="startTimer"
改为onclick="startTimer()"
这将执行该功能。
关于javascript - 如何在 jsfiddle 中使用函数声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20499423/