最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How does $(this) work in jQuery - Stack Overflow

programmeradmin3浏览0评论

How does the jQuery tag $(this) exactly work? I know how to use it, but how does jQuery know which element is 'active'? And what is the original Javascript tag for getting the current item, or is it jQuery only?

How does the jQuery tag $(this) exactly work? I know how to use it, but how does jQuery know which element is 'active'? And what is the original Javascript tag for getting the current item, or is it jQuery only?

Share Improve this question edited Feb 4, 2012 at 10:42 Konerak 39.8k12 gold badges101 silver badges121 bronze badges asked Jul 29, 2010 at 14:09 TimTim 9,4891 gold badge34 silver badges48 bronze badges 4
  • 7 @qor You linked to the current question, so yes – Michael Mrozek Commented Jul 29, 2010 at 14:12
  • Nice to see SO protects closing a question because of being a duplicate of itself though - some sites wouldn't ;) – Konerak Commented Jul 29, 2010 at 14:16
  • possible duplicate of Why do I have to use $(this) – jAndy Commented Jul 29, 2010 at 14:20
  • 1 @Konerak IIRC you can still close two questions as duplicates of each other though – Michael Mrozek Commented Jul 29, 2010 at 14:21
Add a ment  | 

4 Answers 4

Reset to default 7

The this is a simple javascript (DOM) object, $(this) will turn the object into a jQuery object.

jQuery doesn't need to 'know' what this is, it doesn't treat this in a special way, no other than myHeaderDiv in

var myHeaderDiv = document.getElementById('header'); 
$myHeaderDiv = $(myheaderDiv); //just a variable transformed into jQuery object, as with this.

this is context-dependent in jQuery (and JavaScript in general). It usually represents the current DOM element in a event handler, but is not a jQuery object.

$(this) is a jQuery object containing the current DOM element.

The expression $(this) is just a regular Javascript function call, equivalent to jQuery(this). The this value is defined by Javascript itself, and is not a jQuery invention.

Perhaps you should read about scope in JavaScript http://www.digital-web./articles/scope_in_javascript/

发布评论

评论列表(0)

  1. 暂无评论