如何使用 jQuery 從父節點中移除所有子節點?
使用 jQuery empty() 方法可以從父節點中移除所有子節點。
示例
可以嘗試執行以下程式碼,使用 jQuery 從父節點中移除所有子節點
<!DOCTYPE html>
<html>
<head>
<style>
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#button1").click(function(){
$("ul").empty();
});
});
</script>
</head>
<body>
<div>
<ul>
<li>li (child)</li>
<li>li (child)</li>
<li>li (child)</li>
<li>li (child)</li>
</ul>
</div>
<button id="button1">Remove</button>
<p>Click Remove to remove all child nodes.</p>
</body>
</html>
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP