星空网 > 软件开发 > Java

jQuery的remove()方法

remove()方法的定义和用法:

此方法将会从DOM中删除所有匹配的元素。

说明:remove()方法不会把匹配的元素从jQuery对象中删除,因而可以在将来再使用这些匹配的元素,不过除了这个元素本身得以保留之外,其他的比如绑定的事件,附加的数据等都会被移除。

语法结构:

$(selector).remove(expr)

参数列表:

参数描述
expr可选。用于筛选元素的jQuery表达式

实例代码:

<!DOCTYPE html><html><head><meta charset="utf-8"><meta name="author" content="http://www.51texiao.cn/" /><title>蚂蚁部落</title><script type="text/javascript" src='/images/loading.gif' data-original="mytest/jQuery/jquery-1.8.3.js"></script><script type="text/javascript"> $(document).ready(function(){ $("button").click(function(){  $("div").remove("#first"); })})</script> </head><body><div id="first">我是第一</div><div id="second">我是第二</div><button>点击</button></body></html>

以下代码能够删除div集合中id为first的div。

<!DOCTYPE html><html><head><meta charset="utf-8"><meta name="author" content="http://www.51texiao.cn/" /><title>蚂蚁部落</title><script type="text/javascript" src='/images/loading.gif' data-original="mytest/jQuery/jquery-1.8.3.js"></script><script type="text/javascript"> $(document).ready(function(){ $("#btd").click(function(){  $("div").remove(); })})</script> </head><body><div id="first">我是第一</div><div id="second">我是第二</div><button id="btd">点击删除第一个div</button></body></html>

如果省略参数,那就是删除所有匹配的元素。

<!DOCTYPE html><html><head><meta charset="utf-8"><meta name="author" content="http://www.51texiao.cn/" /><title>蚂蚁部落</title><style type="text/css">div{ width:200px; height:200px; border:5px solid green;}</style><script type="text/javascript" src='/images/loading.gif' data-original="mytest/jQuery/jquery-1.8.3.js"></script><script type="text/javascript"> $(document).ready(function(){ $("#btd").click(function(){  var a=$("div");  a.remove("#first");  $("#btn").click(function(){   alert(a.length);  }) })})</script> </head><body><div id="first">我是第一</div><div id="second">我是第二</div><button id="btd">删除第一个div</button><button id="btn">查看删除操作后div的数量</button></body></html>

remove()已经将DOM中的匹配元素删除,但是并没有将它从jquery对象中删除。

原文地址是:http://www.51texiao.cn/jqueryjiaocheng/2015/0613/4051.html

最为原始地址是:http://www.softwhy.com/forum.php?mod=viewthread&tid=5425




原标题:jQuery的remove()方法

关键词:jquery

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流