jQuery methods not working on 'this' inside an event handler

前端 未结 2 920
失恋的感觉
失恋的感觉 2021-01-21 11:37

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

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

相关标签:
2条回答
  • 2021-01-21 12:28

    Modify your code from that :

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

    To This:

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

    Should work now.

    0 讨论(0)
  • 2021-01-21 12:35

    Replace

    this.hide(); 
    

    with

    $(this).hide();
    

    Thus your function should be like

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

    See the official documentation here

    0 讨论(0)
提交回复
热议问题