在 jQuery 中,將一個 H1 元素及其文字放在一個特定的索引處
要設定一個元素及其文字,在 jQuery 中使用 text() 方法。然後,使用 :nth-child 選擇器將它放在特定的位置。以下是程式碼 −
示例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initialscale=1.0">
<title>Document</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
</head>
<body>
<h1 class='demo'>JavaScript</h1>
<h1 class='demo'>MySQL</h1>
<h1 class='demo'>MongoDB</h1>
<h1 class='demo'>Java</h1>
<h1 class='demo'>C#</h1>
<script>
$('h1:nth-child(4)').text("Python");
</script>
</body>
</html>要執行上述程式,請儲存檔名 “anyName.html(index.html)” 並右鍵單擊該檔案。在 VS Code 編輯器中選擇 “用 Live Server 開啟” 選項。
輸出
這將產生以下輸出 −

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