为什么我的jsfiddle Javascript类未定义? [英] Why is my jsfiddle Javascript class not defined?

查看:66
本文介绍了为什么我的jsfiddle Javascript类未定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HTML:

<img src="https://help.pace.edu/helpdesk/info_icon_small.gif?v=12_1_0_300.gif" onclick="plusButton_Click()">

js:

function plusButton_Click() {  
    alert('hi');    
}

http://jsfiddle.net/uEypH/1/

我可能不是Java语言和其他所有语言的新手.但是我的Firefox控制台为什么说

I might be new to Javascript and all. But why does my Firefox console say

"ReferenceError: plusButton_Click is not defined"?

推荐答案

因为如果要在嵌入式js中调用该函数,则该函数必须在全局范围内.

Because the function must be in global scope if you intend to call it in inline js.

jsfiddle创建一个新的闭包,因此它不是全局的.使用其--wrap it in head--选项.

jsfiddle creates a new closure, so it is not global. Use their --wrap it in head-- option.

更新的演示

或更改

function plusButton_Click() {  
    alert('hi');    
}

收件人:

window.plusButton_Click = function(){
   alert('hi');
}

这篇关于为什么我的jsfiddle Javascript类未定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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