【问题标题】:jQuery ~ Function not workingjQuery ~ 功能不工作
【发布时间】:2015-03-25 12:57:21
【问题描述】:

我试图在点击时调用我的 javascript 函数:

<div id='alert'><button onclick='alertClose()' class='alert'>Close</button></div>

函数如下:

function alertClose(){
    $("#alert").animate({
        left: "107%"
    });
}

编辑:控制台一直说没有名为 alertClose 的函数

【问题讨论】:

  • 您的问题是什么?你看到了什么错误?
  • 它做什么而不是工作?:) 任何错误消息?函数是被调用了,但没有做你想做的事,还是根本没有被调用?
  • 这里是网站:chat-trial-silverneon123-2.c9.io 警报在右下角,按钮是我遇到的问题
  • 您需要改进问题,而不是添加其他人以后无法重现问题的链接
  • 我的猜测是这个错误:Uncaught TypeError: Cannot read property 'innerHTML' of undefined 导致你的 js 失败。只是一个想法。另外,如果您使用的是 jquery,为什么不这样做: $("#alert button").click(alertClose);而不是在 html 中声明它。

标签: javascript jquery


【解决方案1】:

我将发布我认为是您的问题:

首先,您在此处的 ajax.js 中遇到 js 错误:

var room = document.getElementsByClassName("selected")[0].innerHTML;

document.getElements 调用没有返回任何内容并且失败

然后,您需要调整代码以使用 jquery 而不是内联事件:

HTML:

<div id='alert'><button class='alert'>Close</button></div>

JS:

function alertClose(){
    $("#alert").animate({
        left: "107%"
    });
}
 $("#alert button").click(alertClose); 

尝试一下,看看它是如何工作的。这是Fiddle

编辑:删除了错误留下的内联函数

【讨论】:

  • 你离开了内联事件,所以函数会触发两次
  • 抱歉,刚刚修好了。
【解决方案2】:

调用函数

function alertClose();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-13
    • 1970-01-01
    • 2013-07-31
    • 1970-01-01
    • 1970-01-01
    • 2014-12-13
    • 1970-01-01
    相关资源
    最近更新 更多