Home  >  Article  >  Web Front-end  >  Detailed explanation of the meaning and usage of hasClass() in jQuery

Detailed explanation of the meaning and usage of hasClass() in jQuery

藏色散人
藏色散人Original
2019-03-28 16:53:219648browse

This article mainly introduces the usage of hasClass() in jQuery. I hope it will be helpful to friends in need!

Detailed explanation of the meaning and usage of hasClass() in jQuery

What does hasclass mean?

hasClass() is a built-in method in jQuery that is used to check whether an element with a specified class name exists.

Syntax:

$(selector).hasClass(className);

Parameters: It accepts a "className" parameter, which specifies the class name that needs to be searched in the selected element.

Return value: If the search is successful, return true, otherwise return false.

jQuery hasClass() method usage example:

<html>

<head>
    <meta charset="UTF-8">
    <script
            src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
    </script>
    <script>
        $(document).ready(function() {
            $("button").click(function() {
                alert($("p").hasClass("find"));
            });
        });
    </script>
    <style>
        .find {
            font-size: 120%;
            color: green;
        }

        body {
            width: 50%;
            height: 200px;
            border: 2px solid green;
            padding: 20px;
        }
    </style>
</head>

<body>

<h1>标题1</h1>

<p class="find">PHP中文网!</p>
<p>这是普通段落</p>

<button>点击我!</button>

</body>

</html>

Output:

Detailed explanation of the meaning and usage of hasClass() in jQuery

When clicking "Click me " button, the following pop-up window appears

Detailed explanation of the meaning and usage of hasClass() in jQuery

Related recommendations: "jQuery Tutorial"

This article is about hasClass in jQuery The meaning and usage of () are explained in detail, simple and easy to understand. I hope it will be helpful to friends in need!

The above is the detailed content of Detailed explanation of the meaning and usage of hasClass() in jQuery. 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