What's the difference between '$(this)' and 'this'?

前端 未结 7 1572
面向向阳花
面向向阳花 2020-11-21 07:18

I am currently working through this tutorial: Getting Started with jQuery

For the two examples below:

$(\"#orderedlist\").find(\"li\").each(function          


        
7条回答
  •  长情又很酷
    2020-11-21 08:05

    When using jQuery, it is advised to use $(this) usually. But if you know (you should learn and know) the difference, sometimes it is more convenient and quicker to use just this. For instance:

    $(".myCheckboxes").change(function(){ 
        if(this.checked) 
           alert("checked"); 
    });
    

    is easier and purer than

    $(".myCheckboxes").change(function(){ 
          if($(this).is(":checked")) 
             alert("checked"); 
    });
    

提交回复
热议问题