JsFiddle中的代码不起作用 [英] Code in the JsFiddle is not working

查看:166
本文介绍了JsFiddle中的代码不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个查询,因为我想准备一个JSFiddle。但它不适用于小型点击程序。其中只有两行代码。



HTML

 <身体GT; 
< button onclick =javascript:launchdialog();>点击我< /按钮>
< / body>

JavaScript

 函数launchdialog(){
alert('test');
}

我在两行代码中没有发现任何错误。
请看这个 JSFiddle - http://jsfiddle.net/g47qqzra/

解决方案

您可以在head或body中设置 No wrap 选项。



当您使用 onload 或 onDomReady ,您的代码被封装在另一个函数中,该函数在 load 准备好事件。因此,您的功能不能从该功能以外访问。而且你会得到错误

lockquote
ReferenceError:functionName没有定义

使函数无包装使它成为全局,可以从任何地方访问。



更新的小提琴



Jsfiddle Doc


I have one query, for that I wanted to prepare a JSFiddle. But it's not working for a small click program. Which has just two lines of code.

HTML

<body>
    <button onclick="javascript:launchdialog();">Click Me</button>
</body>

JavaScript

function launchdialog() {
    alert('test');
}

I didn't find anything wrong in just two lines of code. Please have a look at this JSFiddle - http://jsfiddle.net/g47qqzra/

解决方案

You can set the option to No wrap in head or body.

When you use onload or onDomReady, your code is wrapped inside another function that is invoked on load or ready event. So, your function is not accessible from outside of that function. And you'll get error

ReferenceError: functionName is not defined

Making the function no wrap makes it global, can be accessed from anywhere.

Updated fiddle

Jsfiddle Doc

这篇关于JsFiddle中的代码不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆