Home>Article>Web Front-end> Can jquery use this?

Can jquery use this?

青灯夜游
青灯夜游 Original
2022-12-12 18:03:09 2474browse

jquery can use this. In jquery, this indicates that the current context object is an html object, and you can call the properties and methods owned by the html object; you can use the "$(this)" statement to turn the html element into a jquery object, and then use the jquery method to process the current Object, the syntax is "$(this).jquery method name();".

Can jquery use this?

The operating environment of this tutorial: windows7 system, jquery3.6.1 version, Dell G3 computer.

jquery can use this.

In jquery, this indicates that the current context object is an html object, and you can call the properties and methods owned by the html object.

For example:

this.className this.style.display this.id = 'header' this.remove()

Specific example:

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

This above is an Html element (textbox), and textbox has a text attribute, so you can use this directly. Title = 'test' is assigned, and there is no problem with writing it this way. [Recommended learning:jQuery Video Tutorial]

But if you replace this with $(this), it will not be the case, and an error will be reported. The following writing is wrong:

$("#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 this writing is wrong. JQuery has the attr() method that can get/set attributes of DOM objects, so the correct way to write it should be like this:

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

The advantage of using JQuery is that it packages the operations of various browser versions on DOM objects, so It would be a better choice to use $(this) uniformly instead of this.

Explanation:

##$()is equivalent toJQuery()in JQuery, that is,$( this)=jquery();In other words, this can return a jquery object. Then, when youalert($('#id'));on the web page, a[object Object]will pop up. This object object is also a jquery object.

Examples are as follows

   123   

如果您点击我,我会消失。

点击我,我会消失。

也要点击我哦。

Can jquery use this?

[Recommended learning:

jQuery video,web front-end development]

The above is the detailed content of Can jquery use this?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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
Previous article:Is jquery a script library? Next article:Is jquery a script library?