jQuery $(this) keyword

后端 未结 6 1138
没有蜡笔的小新
没有蜡笔的小新 2020-11-27 15:17

Why is it important to use $(this) instead of re-selecting the class?

I am using a lot of animate and css editing in my code, and I know I can simplify it by using

6条回答
  •  北海茫月
    2020-11-27 15:55

    $(document).ready(function(){
       $('.somediv').click(function(){
       $(this).addClass('newDiv'); // this means the div which is clicked
       });                         // so instead of using a selector again $('.somediv');
    });                           // you use $(this) which much better and neater:=)
    

提交回复
热议问题