Home > Web Front-end > JS Tutorial > How to use the hasClass() method in jquery

How to use the hasClass() method in jquery

王林
Release: 2020-11-30 14:56:42
Original
2841 people have browsed it

The usage of the hasClass() method in jquery is: [$().hasClass("class name")]. The hasClass method is generally used to determine whether the element contains the specified class name. If it does, it returns true, if it does not, it returns false.

How to use the hasClass() method in jquery

The operating environment of this tutorial: Windows 10 system, jquery version 1.12.4. This method is suitable for all brands of computers.

Related recommendations: "jQuery Video Tutorial"

Class name filtering refers to filtering based on the class of the element. In jQuery, we can use the hasClass() method to implement class name filtering.

Syntax:

$().hasClass("类名")
Copy after login

hasClass() method is generally used to determine whether the element contains the specified class name: if it does, it returns true; if it does not, it returns false.

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
    <script src="js/jquery-1.12.4.min.js"></script>
    <script>
        $(function () {
            $("li").each(function(){
                var bool = $(this).hasClass("select");
                if(bool){
                    $(this).css("color", "red");
                }
            })
        })
    </script>
</head>
<body>
    <ul>
        <li>HTML</li>
        <li>CSS</li>
        <li>JavaScript</li>
        <li class="select">jQuery</li>
        <li>Vue.js</li>
    </ul>
</body>
</html>
Copy after login

Effect:

How to use the hasClass() method in jquery

$(this).hasClass("select") is used to determine whether the current li element contains the class name "select". Everyone should note here that the hasClass() method is generally used to implement judgment operations.

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

The above is the detailed content of How to use the hasClass() method in jquery. 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