在 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 開啟” 選項。

輸出

這將產生以下輸出 −

更新於: 2020-09-10

464 次瀏覽

開啟您的 職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.