使用示例講解 jQuery detach()
jQuery 中的 detach() 方法用於移除選定的元素。
語法
語法如下 −
$(selector).detach()
示例
現在我們來看一個實現 jQuery detach() 方法 的示例 −
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("span").detach();
});
});
</script>
<style>
span {
background-color: red;
color: white;
}
</style>
</head>
<body>
<h2>Demo Heading</h2>
<p>This is a <span>demo</span> text.</p>
<button>Remove element</button>
</body>
</html>輸出
這將會產生以下輸出 −

點選“移除元素” −

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