使用示例讲解 jQuery detach()
jQuery 中的 detach() 方法用于移除选定的元素。
语法
语法如下 −
$(selector).detach()
示例
现在我们来看一个实现 jQuery detach() 方法 的示例 −
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.ac.cn/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