Difference between Query-This and $ (this)

Source: Internet
Author: User

At first, I thought that this and $ (this) were carved out. But when I was reading, I found that coding was not the same thing.

What is "this "?

In jsonobject-oriented
Programming versions, this (or self) is a keyword which can be used in
Instance methods to refer to the object on which the currently executing
Method has been invoked.

 

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

Here this is actually an HTML element (textbox). textbox has the text attribute, so there is no problem in writing this.
But if you replace this with $ (this), that's not the case.Error -- reported.

error code:
$ ("# textbox "). hover (
function () {
$ (this ). title = "test";
},
function () {
$ (this ). title = "OK";
}
);

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

Jquery has the ATTR () method that can get/set the attributes of DOM objects, so the correct syntax should be as follows:

correct Code :
$ (" # textbox "). hover (
function () {
$ (this ). ATTR ('title', 'test');
},
function () {
$ (this ). ATTR ('title', 'OK');
}
);

the advantage of using jquery is that it includes operations performed on DOM objects by various browser versions, so $ (this) is used in a unified manner) it should be a good choice to stop using this.

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.