Home >Web Front-end >JS Tutorial >How to add class in jquery

How to add class in jquery

WBOY
WBOYOriginal
2021-12-13 14:41:0516467browse

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");".

How to add class in jquery

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:

How to add class in jquery

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!

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