Home >Web Front-end >JS Tutorial >How to use jQuery hasClass()
The jQuery hasClass() method is used to check whether the selected element contains the specified class name. Its usage syntax is "$(selector).hasClass(classname)". The parameter classname indicates that it needs to be searched in the selected element. the type.
Recommended: "jquery video tutorial"
jQuery hasClass() definition and usage
hasClass() method checks whether the selected element contains the specified class name.
If the selected element contains the specified class, this method returns "true".
Syntax
$(selector).hasClass(classname)
Parameters
classname required. Specifies the class to be searched for in the selected elements.
Example
Check whether the
element contains "intro" class:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>PHP中文网</title> <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"> </script> <script> $(document).ready(function(){ $("button").click(function(){ alert($("p").hasClass("intro")); }); }); </script> <style type="text/css"> .intro{ font-size:120%; color:red; } </style> </head> <body> <h1>这是一个段落标题</h1> <p class="intro">这是一个段落</p> <p> 这是另外一个段落</p> <button>是否有 p 元素使用了 "intro" 类?</button> </body> </html>
Running effect:
The above is the detailed content of How to use jQuery hasClass(). For more information, please follow other related articles on the PHP Chinese website!