Differences between this and $ (this) in JQuery _ jquery

Source: Internet
Author: User
This article describes the differences between this and $ (this) in JQuery. this indicates that the current context object is an html object and can call the attributes and methods of an html object, $ (this) indicates that the context object is a jquery context object, which can call jquery methods and attribute values. If you need it, you can refer to this rarely used in jquery. When the code is used, the value of this is debugged. It is quite useful. Here we will summarize the differences between this and $ (this) and their usage.

What is $ (this) generated?

What is $ () generated? In fact, $ () = jquery () is returned as a jquery object.

We usually use $ () for convenience (). In fact, this function omitting the context parameter. Select the matched object $ (selector, context) based on the selector and return it in the form of a jQuery package set.

Context can be a Dom object set or a jQuery package set. If it is passed in, it indicates that you want to select a matching object from the context. If it is not passed in, it indicates that the range is a Document Object (that is, all objects on the page ), that is, $ (selector) = $ (selector, document ).

This refers to the html object that calls the function.

Example:

The Code is as follows:


$ ("# Textbox"). hover (
Function (){
This. title = "Test ";
},
Fucntion (){
This. title = "OK ";
}
);

Here this is actually an Html element (textbox), and this is in js. Textbox has the text attribute, so there is no problem with this writing.

The Code is as follows:


$ ("# Textbox"). hover (
Function (){
$ (This). title = "Test ";
},
Function (){
$ (This). title = "OK ";
}
);

$ (This) is a JQuery object, and the jQuery object does not have the title attribute. Therefore, this write is incorrect.

Conclusion:

This indicates that the current context object is an html object and can call the attributes and methods of the html object.
$ (This) indicates that the context object is a jquery context object. You can call jquery methods and attribute values.

Instance (Tab ):

The Code is as follows:


Tabs ($ ("# nav a"), $ (". content "));
Function tabs (tab, content ){
Content. hide ();
Content. eq (0). show ();
Tab. click (function (){
Var index = tab. index (this );
Tab. removeClass ("current ");
$ (This). addClass ("current ");
Content. hide ();
Content. eq (index). animate ({opacity: 'show'}, 200 );
});
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.