Home >Web Front-end >JS Tutorial >How to add class in jquery
How to add class in jquery: 1. Use the "$(specified element)" statement to obtain the specified element object; 2. Use the addClass() method to add a class to the specified element. The syntax is "element object.addClass( "new class");".
The operating environment of this tutorial: windows10 system, jquery3.2.1 version, Dell G3 computer.
How to add class in jquery
jquery provides an addClass() method to add a class. You can use this method to class tags Class added. Add a lass class by clicking the button to make the font larger to demonstrate the addClass() method.
addClass() method adds one or more classes to the selected element.
This method does not remove the existing class attribute, but only adds one or more class attributes.
The example is as follows:
<html> <head> <script type="text/javascript" src="/jquery/jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("button").click(function(){ $("p:first").addClass("intro"); }); }); </script> <style type="text/css"> .intro{ font-size: 40px; } </style> </head> <body> <h1>This is a heading</h1> <p>This is a paragraph.</p> <p>This is another paragraph.</p> <button>向第一个 p 元素添加一个类</button> </body> </html>
Output result:
Related video tutorial recommendation: jQuery video tutorial
The above is the detailed content of How to add class in jquery. For more information, please follow other related articles on the PHP Chinese website!