Home >Web Front-end >JS Tutorial >How to use the hasClass() method in jquery
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.
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("类名")
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>
Effect:
$(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!