简体   繁体   English

使用onclick在变量对象中调用函数?

[英]Call function in variable object using onclick?

Okay so here is my problem my javascript is so, 好的,这是我的问题,我的JavaScript是这样,

var example = {
    meep: function() {
            alert("This alert or function does not happen using onclick D:");
    }
}

Looks fine right? 看起来不错吧? Okay so now when I do this, 好吧,现在当我这样做时,

<button onclick="example.meep()"></button>

It does not work and returns "Uncaught TypeError: undefined is not a function" I can also do the function "example.meep()" in console and it works. 它不起作用,并返回“ Uncaught TypeError:未定义不是函数”,我也可以在控制台中执行函数“ example.meep()”,并且它可以工作。 I've tried it with and without ending parentheses. 我已经试过了,没有加括号。

try attaching the example to the window. 尝试将示例附加到窗口。 Change 更改

var example = {
  ...

to

window.example = {
  ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM