Home > Web Front-end > JS Tutorial > How to use jquery $(this)

How to use jquery $(this)

藏色散人
Release: 2021-01-08 14:27:21
Original
3045 people have browsed it

jquery $(this) is usually a JQuery object, which can call jquery methods and attribute values. Use syntax such as "$(this).attr(key);" to obtain the value of the node attribute name.

How to use jquery $(this)

The operating environment of this tutorial: windows7 system, jquery1.2.6 version, DELL G3 computer.

Recommended: jquery tutorial

How to use jquery $(this)?

This is usually an Html element, such as (textbox), and textbox has a text attribute. You can refer to this in the textbox event to get the element

$(this) is usually a JQuery object, and you can call jquery methods and attribute values, such as click(), keyup().

$(function () {
    $('button').click(function () {
       $(this)表示当前对象,这里指的是button
       //alert(this);//this 表示原生的DOM
   }) 
});
Copy after login

$(this).attr(key); Get the value of the node attribute name, equivalent to the getAttribute(key) method

$(this).attr(key, value); Set The value of the node attribute is equivalent to the setAttribute(key, value) method

$(this).val(); to obtain the value of an element node, which is equivalent to $(this).attr("value" );

$(this).val(value);Set the value of an element node, equivalent to $(this).attr(“value”,value);

Example :

$("#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 should be a good choice to use $(this) uniformly instead of this.

$() What does it generate?

Actually $()=jquery(), which means that a jquery object is returned.

According to the conclusion that $() returns a jquery object, we can conclude that $(this) returns a jquery object. We can use the universal alert() method to print out an object:

alert($('#btn'));
Copy after login

Displayed results:

How to use jquery $(this)

The red box in the picture is an object. Don’t think about it. The object is naturally a jquery object. That is to say, we use $('#btn') to call jquery's methods and properties.

For more programming-related knowledge, please visit: Programming Teaching! !

The above is the detailed content of How to use jquery $(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