javascript - 如何在 jsfiddle 中使用函数声明

标签 javascript jsfiddle

我的 .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 .

JSFiddle Option

描述

onLoadwindow.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/

相关文章:

javascript - 选择未使用 ng-model 选择正确的选项

javascript - 为什么我的 Jasmine 规范认为我的 Angular 模块未定义

javascript - 如何解决在 Android 中运行 NativeScript 应用程序时出现的问题?

firefox - console.log 没有显示在我的 Firebug 中

twitter-bootstrap - 在 Vuejs 单文件组件中包含 bootstrap.js 外部脚本

css - 使用 CSS3 在图像悬停时淡入淡出?

javascript - 有一个 CSSStyleDeclaration 对象我能找到它属于哪个节点吗?

javascript - 类更改不会影响 onmouseover

javascript - 尝试用复选框过滤结果

typescript - TypeScript 字符串枚举中的反向映射