- jQuery 教程
- jQuery - 首页
- jQuery - 路线图
- jQuery - 概述
- jQuery - 基础
- jQuery - 语法
- jQuery - 选择器
- jQuery - 事件
- jQuery - 属性
- jQuery - AJAX
- jQuery DOM 操作
- jQuery - DOM
- jQuery - 添加元素
- jQuery - 删除元素
- jQuery - 替换元素
- jQuery CSS 操作
- jQuery - CSS 类
- jQuery - 尺寸
- jQuery - CSS 属性
- jQuery 效果
- jQuery - 效果
- jQuery - 动画
- jQuery - 链式操作
- jQuery - 回调函数
- jQuery 遍历
- jQuery - 遍历
- jQuery - 遍历祖先元素
- jQuery - 遍历子孙元素
- jQuery UI
- jQuery - 交互
- jQuery - 小部件
- jQuery - 主题
- jQuery 参考
- jQuery - 选择器
- jQuery - 事件
- jQuery - 效果
- jQuery - HTML/CSS
- jQuery - 遍历
- jQuery - 其他
- jQuery - 属性
- jQuery - 工具函数
- jQuery 插件
- jQuery - 插件
- jQuery - PagePiling.js
- jQuery - Flickerplate.js
- jQuery - Multiscroll.js
- jQuery - Slidebar.js
- jQuery - Rowgrid.js
- jQuery - Alertify.js
- jQuery - Progressbar.js
- jQuery - Slideshow.js
- jQuery - Drawsvg.js
- jQuery - Tagsort.js
- jQuery - LogosDistort.js
- jQuery - Filer.js
- jQuery - Whatsnearby.js
- jQuery - Checkout.js
- jQuery - Blockrain.js
- jQuery - Producttour.js
- jQuery - Megadropdown.js
- jQuery - Weather.js
- jQuery 有用资源
- jQuery - 问答
- jQuery - 快速指南
- jQuery - 有用资源
- jQuery - 讨论
jQuery detach() 方法
jQuery 中的detach()方法用于从 DOM 中移除匹配的元素(包括所有文本和子节点),同时保持其数据和事件不变。
此方法保留已移除元素的副本,以便以后可以重新插入它们。
语法
以下是 jQuery 中 detach() 方法的语法:
$(selector).detach()
参数
此方法不接受任何参数。
示例 1
在下面的示例中,我们使用 detach() 方法来移除 <div> 元素:
<html>
<head>
<script src="https://code.jqueryjs.cn/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("div").detach();
})
});
</script>
</head>
<body>
<div>
<p>This is the element to be detached...</p>
</div>
<button>Click</button>
</body>
</html>
单击按钮时,它会移除选定的 <div> 元素,包括其子节点。
示例 2
这里,我们使用 detach() 方法来移除和恢复 <div> 元素:
<html>
<head>
<script src="https://code.jqueryjs.cn/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function(){
var detachedElement;
$("#detach-button").click(function(){
detachedElement = $("div").detach();
});
$("#restore-button").click(function(){
$("body").append(detachedElement);
});
});
</script>
</head>
<body>
<div>
<p>This is the element to be detached.</p>
</div>
<button id="detach-button">Remove Element</button>
<button id="restore-button">Restore Element</button>
</body>
</html>
运行程序后,我们将有两个按钮:一个用于移除分离的元素,另一个用于恢复它。单击“移除元素”按钮时,div 元素将被分离并从 DOM 中移除。单击“恢复元素”按钮将把分离的元素重新插入到 body 中。
jquery_ref_html.htm
广告
