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

Source: Internet
Author: User

What is the following two code to see on jquery's website, when to use $ (this), and when to use this?

$ (document). Ready (function () {   $ ("#orderedlist li:last"). Hover (function () {     $ (this). AddClass ("green");   , function () {     $ (this). Removeclass ("green"); # $ (this)   });});
$ (document). Ready (The function () {//Use the-to-   reset several forms at once   $ ("#reset"). Click (function () {     $ (" Form "). each (function () {       this.reset (); # this});})   ;

If you want to use the properties or methods of the HTML element itself to use this, if you want to use jquery wrapped method or property to use $ (this), generally have the following relationship

$ (this) [0]==this;

The code above is to use this where the Reset method is to invoke form form, and this method jquery does not have wrapper support, so only this.reset (), can also use $ (this) [0].reset ();

Meaning this is an HTML element object

$ (this) becomes a jquery object

This is a keyword in JavaScript.
$ (this) can be thought of as wrapping the this in JavaScript, and wrapping the $ (this) will inherit the jquery method.

The essence is the translation of JavaScript and jquery objects

$ (' a '). Click (function () {     //) Here this points to the currently clicked Dom node, which is a. You can call DOM methods, such as This.getattribute, This.tagname    ... The $ (this) here represents a wrapped jquery object that has some of the methods of jquery, such as $ (this). AddClass (), $ (this). Hide () ...});

$ (this) is a jquery object that can invoke the method of jquery, such as Click (), KeyUp ().
This, then, is an HTML element object that can invoke element attributes, such as This.id,this.value.
For example, if you have made this and $ (this) point to the input object, to get the value of input, you can this.value, but $ (this) will be $ (this). Val ().

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

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.