Jquery empty() remove() detach() 方法的区别
方法简介:
empty()
This method removes not only child (and other descendant) elements, but also any text within the set of matched elements. This is because, according to the DOM specification, any string of text within an element is considered a child node of that element.
该方法不仅删除它的子节点,同时也删除该节点的文本域(根据DOM规范,节点的文本域也被认为是子节点)。
remove( [selector] )
selectorA selector expression that filters the set of matched elements to be removed.
Similar to .empty(), the .remove() method takes elements out of the DOM. Use.remove() when you want to remove the element itself, as well as everything inside it. In addition to the elements themselves, all bound events and jQuery data associated with the elements are removed. To remove the elements without removing data and events, use.detach() instead.
和empty方法类似,remove方法也是从DOM里删除元素。当你想要删除节点本身和节点里的所有东西的时候,可以使用remove方法。除了节点本身以外,节点绑定的事件 和该节点相关的JQuery数据,也会被同时清除。当需要清除节点本身,但是不需要清除绑定的事件和数据的时候,可以使用detach方法。
detach( [selector] )
selectorA selector expression that filters the set of matched elements to be removed.
The .detach() method is the same as .remove(), except that .detach() keeps all jQuery data associated with the removed elements. This method is useful when removed elements are to be reinserted into the DOM at a later time.
detach方法和remove方法很相似,但是它会保留所有JQuery相关的数据和绑定的事件。当你删除之后,想要在后来的某个时候重新加入时,这个方法将会很有用。
三者区别:
empty,remove,detach方法的区别
|
方法名称 |
元素本身(包含所有属性) |
绑定事件和JQuery相关数据 |
文本域及子节点 |
empty() |
不删除 |
不删除 |
删除 |
remove() |
删除 |
删除 |
删除 |
detach() |
删除 |
不删除 |
删除 |
示例验证:
验证环境: JQuery-1.8.0.min.js, Firefox 11.0, Firebug1.9.2;
1、验证是否删除元素本书(包含所有属性)、文本域及子节点
代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Test Jquery remove detach empty</title>
<style>p { background:yellow; margin:6px ; } p.off { background: black; }</style>
<script src="js/jquery-1.8.0.min.js"></script>
<script>
$(document).ready(function(){
$("p").click(function(){
$(this).toggleClass("off");
}); var p;
$("#button").click(function(){
if ( p ) {
p.appendTo("body");
p = null;
} else {
p = $("p").detach();
// p = $("p").remove();
// p = $("p").empty();
}
});
});
</script>
</head> <body>
<p id="A">Hello <span>subNode</span></p>
how are
<p id="B">you? <span>subNode</span></p> <input type="button" id="button" value="Attach/detach paragraphs" /> </body>
</html>
detach() 方法:
执行之前的HTML DOM结构如图:

执行删除之后的 HTML DOM结构如图:
观察可知使用detach方法,删掉了元素<p>本身即它的所有属性(id 等), 文本域及其子节点<span>subNode</span>.
其他方法,类似可以验证,这里就不赘述。
2、验证绑定的事件和JQuery相关数据
这里需要说明的是: 绑定事件,指的是JQuery动态绑定的事件,不是通过元素的属性指定的事件。(下面会举例说明)
JQuery 相关数据,不太懂指的什么,没有找到相关示例,希望有识之士不吝赐教。
下面的示例仅针对绑定事件,便于大家理解:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Test Jquery remove detach empty</title>
<style>
p {
background:yellow;
margin:6px ;
}
p.off {
background: black;
}
</style>
<script src="js/jquery-1.8.0.min.js"></script>
<script>
$(document).ready(function(){ // JQuery 为P标签动态绑定click事件
$("p").click(function(){
$(this).toggleClass("off");
}); var p;
$("#button").click(function(){
if ( p ) {
p.appendTo("body");
p = null;
} else {
p = $("p").detach();
// p = $("p").remove();
// p = $("p").empty();
}
});
});
</script>
</head> <body>
<p id="A">Hello <span>subNode</span></p>
how are
<p id="B">you? <span>subNode</span></p> <input type="button" id="button" value="Attach/detach paragraphs" /> </body>
</html>
运行该页面,点击按钮,删除; 再次点击按钮,删除的<p>标签,重新加入到body的底部,这里我们重点验证的是,重新加入后的绑定事件click是否有效【这里的click事件为,点击P标签,class在黄色和黑色之间切换】。
1) 验证detach方法
可以看到执行detach方法,重新添加之后,JQuery动态绑定的click事件,toggleClass生效,说明之前删除的时候没有把动态绑定的事件删除。
2) 验证remove方法
修改程序中以下部分:
p = $("p").remove();
测试remove函数,同样是先删除在加入,执行完后的效果:
可以发现调用remove方法之后,再重新把删除的标签加入到body后时,发现JQuery动态绑定的事件,已经失效,点击P标签,class不能切换。说明在执行remove的时候,已经把JQuery动态绑定的事件删除了。
3) 验证empty方法:
要验证empty方法,要麻烦一点,在删除之后,重新加入之后,text为空,不能够点击测试click事件,因此我们需要新增一个按钮,为这种情况增加text,便于测试click事件。
代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Test Jquery remove detach empty</title>
<style>
p {
background:yellow;
margin:6px ;
}
p.off {
background: black;
}
</style>
<script src="js/jquery-1.8.0.min.js"></script>
<script>
$(document).ready(function(){ // JQuery 为P标签动态绑定click事件
$("p").click(function(){
$(this).toggleClass("off");
}); var p;
$("#button").click(function(){
if ( p ) {
p.appendTo("body");
p = null;
} else {
// p = $("p").detach();
// p = $("p").remove();
p = $("p").empty();
}
}); // 为删除之后重新加入的p标签加入text
$("#buttonA").click(function(){
$("#A").text("Hello");
$("#B").text("you?");
});
});
</script>
</head> <body>
<p id="A">Hello <span>subNode</span></p>
how are
<p id="B">you? <span>subNode</span></p> <input type="button" id="button" value="Attach/detach paragraphs" /> <br/>
<input type="button" id="buttonA" value="addTextA" /> <br/> </body>
</html>
执行效果如图:
可以发现empty方法,没有删除动态绑定事件。
4) 补充讲解:
为了更好的理解这里所说的绑定事件是JQuery的动态绑定事件,我们修改一下程序,使用onclick属性,把click事件作为一个属性值,来静态绑定。
修改后的程序如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Test Jquery remove detach empty</title>
<style>
p {
background:yellow;
margin:6px ;
}
p.off {
background: black;
}
</style>
<script src="js/jquery-1.8.0.min.js"></script>
<script>
// 处理P标签click事件
function clickHandler(element){
$(element).toggleClass("off");
} $(document).ready(function(){
var p;
$("#button").click(function(){
if ( p ) {
p.appendTo("body");
p = null;
} else {
// p = $("p").detach();
p = $("p").remove();
// p = $("p").empty();
}
}); });
</script>
</head> <body>
<p id="A" onclick="clickHandler(this)">Hello <span>subNode</span></p>
how are
<p id="B" onclick="clickHandler(this)">you? <span>subNode</span></p> <input type="button" id="button" value="Attach/detach paragraphs" /> <br/> </body>
</html>
我们再次使用remove方法,删除然后在append,发现事件响应有效了。其实这里的click事件已经作为一个静态的p元素的一个属性onclick的值了。所有append之后是有效的。
Jquery empty() remove() detach() 方法的区别的更多相关文章
- jquery之remove(),detach()方法详解
一:remove()方法 remove()函数用于从文档中移除匹配的元素. 你还可以使用选择器进一步缩小移除的范围,只移除当前匹配元素中符合指定选择器的部分元素. 与detach()相比,remove ...
- jQuery中detach&&remove&&empty三种方法的区别
jQuery中empty&&remove&&detach三种方法的区别 empty():移除指定元素内部的所有内容,但不包括它本身 remove():移除指定元素内部的 ...
- jquery remove() detach() empty()三种方法的区别
remove方法把事件删除掉了,数据并没有删除 detach方法保存了事件和数据 empty方法保留了元素本身,移除子节点,删除内容 举例: <!DOCTYPE html><html ...
- jQuery.toggleClass() 和detach()方法详解
一.toggleClass()函数: toggleClass()函数用于切换当前jQuery对象所匹配的每一个元素上指定的css类名.所谓"切换",就是如果该元素上已存在指定的类名 ...
- [jQuery]html(),text(),val()方法的区别
1.HTML html():取得第一个匹配元素的html内容.这个函数不能用于XML文档.但可以用于XHTML文档 html(val):设置每一个匹配元素的html内容.这个函数不能用于XML文档.但 ...
- jquery offset() 与position()方法的区别
jquery 中有两个获取元素位置的方法offset()和position(),这两个方法之间有什么异同?使用的时候应该注意哪些问题?什么时候使用offset(),什么时候又使用position()呢 ...
- jquery attr()和prop()方法的区别
$('').attr()返回的是html对象 $('').prop()返回的是DOM对象 attr(): attr() 方法设置或返回被选元素的属性和值. 当该方法用于返回属性值,则返回第一个匹配元素 ...
- jquery 中一些 特殊方法 的特殊使用 一览表
cnblogs的页面, 一种是管理页面, 是随笔的列表 a full list of essays. 另一种是 首页. 要搜索文档的话, 就使用 "首页"的那种方式. 一个jque ...
- jQuery中删除方法empty(),remove()和detach()的区别
empty():清空匹配的元素集合中所有的子节点,自身节点和事件都未被删除. remove():这个方法不会把匹配的元素从jQuery对象中删除,因而可以在将来再使用这些匹配的元素.但除了这个元素本身 ...
随机推荐
- 5. SprigBoot自动配置原理
配置文件到底能写什么?怎么写? 都可以在SpringBoot的官方文档中找到: 配置文件能配置的属性参照 1.自动配置原理: 1).SpringBoot启动的时候加载主配置类,开启了自动配置功 ...
- Javaweb学习笔记——(五)——————DOM&XML目录
1.表单提交方式 *使用submit提交 <form> <input type="submit" /> </form> *使用button提交表 ...
- 2017CCPC秦皇岛 L题One-Dimensional Maze&&ZOJ3992【模拟】
链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3992 题意: 走迷宫,一个一维字符串迷宫,由'L'.'R'组成,分别 ...
- Vim中自动在程序起始处添加版权和作者信息
在编写程序的时候,经常需要在程序开始写上程序的简要介绍和作者信息,如下: 这种信息,除了文件名和修改时间可能经常发生变化外,其他基本不变,可以在程序开始自动加入,方法就是在家目录下的.vimrc中写入 ...
- python中的魔法参数:*args和**kwargs
python中的魔法参数:*args和**kwargs def foo(*args, **kwargs):print 'args = ', argsprint 'kwargs = ', kwargsp ...
- IAR各个历史版本的下载地址
http://supp.iar.com/Updates/?product=EWarm 点击进入上述链接,拉到最底部,点击old version即可见到所有的历史版本!!!
- C语言函数调用栈(三)
6 调用栈实例分析 本节通过代码实例分析函数调用过程中栈帧的布局.形成和消亡. 6.1 栈帧的布局 示例代码如下: //StackReg.c #include <stdio.h> //获取 ...
- dubbo系列三、架构介绍及各模块关系
一.整体设计 图例说明: 图中左边淡蓝背景的为服务消费方使用的接口,右边淡绿色背景的为服务提供方使用的接口,位于中轴线上的为双方都用到的接口. 图中从下至上分为十层,各层均为单向依赖,右边的黑色箭头代 ...
- 020_秘钥管理服务器vault
一. https://github.com/hashicorp/vault #待研究
- 003_Linux的Cgroup<实例详解>
为什么要有cgroup Linux系统中经常有个需求就是希望能限制某个或者某些进程的分配资源.也就是能完成一组容器的概念,在这个容器中,有分配好的特定比例的cpu时间,IO时间,可用内存大小等.于是就 ...