jQuery detach() 方法
实例
移除所有 <p> 元素:
<!DOCTYPE html><html><head><script src="https://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script><script>$(document).ready(function(){$("button").click(function(){$("p").detach();});});</script></head><body><p>这是一个段落</p><p>这是另一个段落</p><button>移除所有 p 元素</button></body></html>
定义与用法
detach() 方法删除选定的元素,包括所有文本和子节点。但是,它保留数据和事件。
此方法还保留已删除元素的副本,以便以后重新插入。
提示:要删除元素及其数据和事件,请改用 remove() 方法。
提示:要仅删除选定元素中的内容,请使用 empty() 方法。
语法
$(selector).detach()