Can jquery use this?

青灯夜游
Release: 2022-12-12 18:03:09
Original
2527 people have browsed it

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()
Copy after login

Specific example:

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

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";
  }
);
Copy after login

$(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');
    }
);
Copy after login

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.

Examples are as follows

<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>
Copy after login

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!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!