在jsfiddle中测试一个函数 [英] testing a function in jsfiddle
问题描述
对不起,这里真是个愚蠢的问题。如果你发布一个关于javascript或jquery的问题,人们常说 - 创建一个jsfiddle。
Sorry, really stupid question here. If you post a question about javascript or jquery, people often say - create a jsfiddle.
所以,我做到了。我的问题是如何使用jquery让divs滑动,但我甚至无法在jsfiddle中运行基本的javascript函数。
So, I did. My question will be about getting divs to slide up using jquery but, I can't even get a basic javascript function to run in jsfiddle.
<div onclick="showit()">Hello</div>
function showit()
{
alert('hi');
}
一个div,点击它来调用javascript函数,但jsfiddle说功能未定义。你能否在jsfiddle中这样做?
A div, click on it to call a javascript function, but jsfiddle says the function is not defined. Can you not do this in jsfiddle?
推荐答案
你选择了默认的 onLoad $ c jsfiddle中的$ c>选项。
这会导致网站将整个代码包装在回调函数中,这意味着你的 showit
function不是DOM0内联事件处理程序所需的全局函数。
This causes the site to wrap your entire code within a callback function, meaning that your showit
function is not a global function as required by DOM0 inline event handlers.
根据我个人的偏好顺序,有几种解决方法:
There are several work arounds, in my personal order of preference:
-
不要使用DOM0内联处理程序,它们真的旧学校 - 查看
element.addEventListener()
而是将您的标记与JS分开。
don't use DOM0 inline handlers, they're really old school - look into
element.addEventListener()
instead and separate your markup from your JS.
使用 window.showit = function(){...}
而不是强制你的函数出现在全局名称空间中。
use window.showit = function() { ... }
instead to force your function to appear in the global name space.
选择其中一个没有换行选项
select one of the "no wrap" options
这篇关于在jsfiddle中测试一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!