jsFiddle:html和js之间没有连接?无法从按钮调用简单的功能? [英] jsFiddle: no connection between html and js? Can't call simple function from button?

查看:213
本文介绍了jsFiddle:html和js之间没有连接?无法从按钮调用简单的功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉,如果这非常明显,但我不知道为什么这不起作用。

Sorry if this is incredibly obvious, but I have no idea why this isn't working.

我正在尝试创建一个jsFiddle。但是,我似乎无法将任何javscript附加到任何元素。例如,onclick不会调用我的js函数。

I'm trying to create a jsFiddle. However, I don't seem to be able to attach any of the javscript to any of the elements. For example, onclick is not calling my js function.

它没有比这个小提琴更简单。我从W3编辑器中复制粘贴它工作得很好 - 点击按钮,获得提醒!

It doesn't get any simpler than this fiddle. I copy-pasted it from the W3 editor where it works just fine - click the button, get alert!

<button onclick="myFunction()">Click me</button>

function myFunction() {alert("sss");}

http://jsfiddle.net/tpip/NYkQN/2/

我在这里缺少什么?

推荐答案

只需改变加载JavaScript块的方式到没有换行(头)

Just change the way the JavaScript block is loaded to no wrap (head)

此处的标准设置是在DOM加载后将JS包装(!) 。这意味着该函数未在全局范围内定义,而是在包装器内定义。内联事件处理程序只能使用全局定义的函数!

The standard setting here is that the JS is wrapped(!) to be run after the DOM has loaded. That means the function is not defined in global scope but inside the wrapper. Inline event handlers only work with globally defined functions though!

设置为时不包装(head) JS是加载在< head> 中并且未包装。

When setting to no wrap (head) the JS is loaded in the <head> and is not wrapped.

这篇关于jsFiddle:html和js之间没有连接?无法从按钮调用简单的功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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