What is the difference between this and $(this) in jQuery?

后端 未结 3 1488
情深已故
情深已故 2021-01-01 05:53

What is the difference between this and $(this) in jQuery? And when should I use which?

3条回答
  •  萌比男神i
    2021-01-01 06:03

    $('p').each(function () {
      //this.id;
      //$(this).attr('id');
    })
    

    If you consider the function above jQuery will loop through each paragraph element on the page and will return a reference to each paragraph element by passing the 'this' variable into the anonymous function. If the 'this' variable is wrapped in the jQuery function ($(this)) then we can access all the jQuery goodness in relation to the element e.g $(this).find('span'). The 'this' object on it's own is just a normal Javscript DOM Object.

提交回复
热议问题