如何使用 jQuery 中的元素 ID 從 DOM 中移除所有元素?
要使用元素 ID 從 DOM 中移除所有元素,請使用 remove() 方法。
示例
你可以嘗試執行以下程式碼,使用元素 ID 從 DOM 中移除所有元素
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#mydiv").remove();
});
});
</script>
</head>
<body>
<button>Click to remove all elements</button><br>
<div id="mydiv" style="height:200px;width:350px;border:2px solid red;background-color:blue;">
<p>This is some text.</p>
<span>This is demo text.</span>
</div>
</body>
</html>
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
JavaScript
PHP