Examples of this and $ (this) difference in jquery _jquery

Source: Internet
Author: User
Tags wrapper

This is rarely used in jquery at ordinary times. When you look at the code, you find it, and you debug the value of this. It's very useful. Here is a summary of the difference and use of this and $ (this).

What is the $ (this) generated?

What is generated by $ ()? actually $ () =jquery (), which means that the object returned is a jquery.

Usually we use $ () directly for simplicity. In fact, the function omits a parameter context. Select the matching object, i.e. $ (selector, context), to be returned as a jquery wrapper set, based on the selector.

The context can be either a collection of Dom objects or a jquery wrapper set, and the incoming means that you want to select a matching object from within, and not pass in the range of the Document object (that is, all the page objects), that is, $ (selector) =$ (selector,document).

This refers to the HTML object that invokes the function.

Example:

Copy Code code as follows:

$ ("#textbox"). Hover (
function () {
This.title = "Test";
},
Fucntion () {
This.title = "OK";
}
);

Here is actually an HTML element (textbox), this is in JS. The textbox has the Text property, so it's OK to write this.

Copy Code code as follows:

$ ("#textbox"). Hover (
function () {
$ (this). title = "Test";
},
function () {
$ (this). title = "OK";
}
);

The $ (this) is a jquery object, and the jquery object has no title attribute, so it is wrong to write it.

Conclusion:

This indicates that the current context object is an HTML object that can invoke the properties and methods owned by the HTML object.
$ (this), the context object represented is a jquery context object that can invoke jquery's method and property values.

Instance (tab):

Copy Code code 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.