使用示例的 jQuery addClass()
jQuery 中的 addClass() 方法用於為選定的元素新增一個或多個類名稱。
示例
現在讓我們看一個示例來實現 jQuery addClass() 方法 −
<!DOCTYPE html>
<html>
<head>
<style>
.demo * {
display: block;
border: 3px dashed red;
padding: 3px;
margin: 25px;
}
.one {
border: 2px solid yellow;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("span.test").next().addClass("one");
});
</script>
</head>
<body>
<h1>Heading One</h1>
<div class="demo" style="width:600px;">This is our demo text in div.
<p>child
<span>grandchild</span>
<span class="test">grandchild</span>
<span>grandchild</span>
</p>
<p>child
<span>grandchild</span>
</p>
</div>
</body>
</html>輸出
這將產生以下輸出 −

現在讓我們看另一個示例來實現 jQuery addClass() 方法 −
<!DOCTYPE html>
<html>
<head>
<style>
.one {
color: white;
background-color: red;
font-size: 18px;
border: 2px blue dashed;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("[id]").addClass("one");
});
</script>
</head>
<body>
<h1>Car Details</h1>
<p id="demo">Car is XUV500</p>
<p>2179 cc</p>
<p>Independent Suspension</p>
<p>Fuel Tank Capacity: 70 litres</p>
</body>
</html>輸出
這將產生以下輸出 −

廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP