如何使用 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>
廣告
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP