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();".
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 to
JQuery() in JQuery, that is,
$( this)=jquery();In other words, this can return a jquery object. Then, when you
alert($('#id')); on the web page, a
[object Object] will pop up. This object object is also a jquery object.
<html> <head> <meta charset="utf-8"> <title>123</title> <script src="https://cdn.staticfile.org/jquery/3.6.0/jquery.min.js"> </script> <script> $(document).ready(function(){ $("p").click(function(){ $(this).hide();//$(this)是在方法click内,此处的$(this)表示的是当前调用click方法的对象$("p"),就是表示当前对象,当前调用该方法的对象 }); }); </script> </head> <body> <p>如果您点击我,我会消失。</p> <p>点击我,我会消失。</p> <p>也要点击我哦。</p> </body> </html>
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!