Home > Web Front-end > JS Tutorial > The difference between JQuery this and $(this)_jquery

The difference between JQuery this and $(this)_jquery

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-05-16 18:47:46
Original
1177 people have browsed it

What is "this"?
In many object-oriented programming languages, 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.

Copy code The code is as follows:

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

This here is actually an Html element (textbox), and textbox has a text attribute, so there is no problem at all writing it this way.
But if this is replaced with $(this), that is not the case, and Error– will be reported.
Error Code:
Copy code The code is as follows:

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

$(this) here is a JQuery object, and the jQuery object does not have a title attribute, so it is wrong to write it like this.

JQuery has the attr() method to get/set attributes of DOM objects, so the correct way to write it should be like this:

Correct code:
Copy code The code is as follows:

$("#textbox").hover(
function() {
$(this ).attr('title', 'Test');
},
function() {
$(this).attr('title', 'OK');
}
);

The advantage of using JQuery is that it packages the operations of DOM objects in various browser versions, so it should be a good choice to use $(this) uniformly instead of this.
Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
Why is this ID written like this?
From 1970-01-01 08:00:00
0
0
0
Small question about $this
From 1970-01-01 08:00:00
0
0
0
What's the use of this?
From 1970-01-01 08:00:00
0
0
0
This file demo.mp4
From 1970-01-01 08:00:00
0
0
0
javascript - es6中this
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template