jQuery方法不能在事件处理程序中的'this'上工作 [英] jQuery methods not working on 'this' inside an event handler

查看:105
本文介绍了jQuery方法不能在事件处理程序中的'this'上工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用下面的代码时,我无法让jQuery this 来隐藏元素。

  $('。purplePanda')。click(function(e){
this.hide();
});

我得到这个错误:

Uncaught TypeError:this.hide不是函数

解决方案

p>

  this.hide(); 

with

  $(本).hide(); 

因此,您的函数应该像

<$ p点击(函数(e){
$(this).hide();
}); $ p $ $('。purplePanda'

查看官方文档 here

When I use the below, I cannot get the jQuery this to hide the element.

$('.purplePanda').click(function(e){
   this.hide();
});

I get this error:

Uncaught TypeError: this.hide is not a function

解决方案

Replace

this.hide(); 

with

$(this).hide();

Thus your function should be like

$('.purplePanda').click(function(e){
   $(this).hide();
});

See the official documentation here

这篇关于jQuery方法不能在事件处理程序中的'this'上工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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