【问题标题】:jQuery : Passing a method directly to the on event handler isn't workingjQuery:将方法直接传递给 on 事件处理程序不起作用
【发布时间】:2013-12-23 19:35:09
【问题描述】:

我正在制作一个带有关闭按钮的简单通知,它应该会淡出:

  this.close.on("click",this.clear);

  }

  Notification.prototype.clear = function(){
    this.node.fadeOut();

  };

但是,当我单击它时,它会抛出此异常“未捕获的 TypeError:无法调用未定义的方法 'fadeOut'”

我尝试传递一个调用 clear 方法的函数表达式,它起作用了。

所以我的问题是为什么它第一次不起作用,但第二次起作用。

【问题讨论】:

  • this.node 是一个 jQuery 对象吗?
  • 是的,它是一个 jQuery 对象

标签: javascript jquery event-handling


【解决方案1】:

你需要使用jquery对象如下:

$(this.node).fadeOut();

提供的节点代表一个 DOM 对象,这将使用 jquery 淡出该对象。

【讨论】:

    猜你喜欢
    • 2016-06-23
    • 2012-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-30
    相关资源
    最近更新 更多