如何在jsfiddle中使用函数声明

如何在jsfiddle中使用函数声明

本文介绍了如何在jsfiddle中使用函数声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的.cshtml文件中有一些可以使用的html/javascript.
当我尝试将其移至jsfiddle中进行试验时,它不起作用.

I have some html/javascript that works in my .cshtml file.
When I try to move it in to jsfiddle to experiment with, it does not work.
Not sure if if it's my lack of javascript experience, jsfiddle experience, probably both....

html:

<div>
<button name="startBtn" id="startBtn" onclick="startTimer">Start</button>
</div>

(我也为onclick属性尝试了"startTimer()";结果相同.)

(I have also tried "startTimer()" for the onclick attribute; same result.)

javascript:

javascript:

function startTimer() { alert("startTimer"); }

当我单击按钮时,我会在控制台中看到它:

When I click the button, I see this in the Console:

我俯瞰什么?
(jsfiddle: http://bit.ly/1buQx9t )

What am I overlooking?
(jsfiddle: http://bit.ly/1buQx9t)

推荐答案

jsFiddle演示

首先,您必须将Framework设置为No wrap - in <head>,而不是onLoad.

jsFiddle Demo

First you have to set the Framework to No wrap - in <head>, instead of onLoad.

说明

onLoad与JavaScript中的window.onload=function(){[YOU ARE TYPING HERE]}或jQuery中的$(function(){[YOU ARE TYPING HERE]});相同.

onLoad is the same as window.onload=function(){[YOU ARE TYPING HERE]} in JavaScript or $(function(){[YOU ARE TYPING HERE]}); in 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 中的设置,这些设置可能会造成混淆.

I hope this clears up the settings in jsFiddle, they can be somewhat confusing.

第二您的HTML略有错误:

HTML

<div>
    <input type="button' value='"Start" onclick="startTimer()"/>
</div>

说明

onclick="startTimer"更改为onclick="startTimer()",这将执行该功能.

onclick="startTimer" was changed to onclick="startTimer()" this will execute the function.

这篇关于如何在jsfiddle中使用函数声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 18:32