jQuery的remove()方法

王朝学院·作者佚名  2016-08-27  
宽屏版  字体: |||超大  

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

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

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

语法结构:

$(selector).remove(exPR)

参数列表:

参数

描述

expr

可选。用于筛选元素的jQuery表达式

实例代码:

<!DOCTYPE html><html><head><metacharset="utf-8"><metaname="author"content="http://www.51texiao.cn/"/><title>蚂蚁部落</title><scripttype="text/javascript"src="mytest/jQuery/jquery-1.8.3.js"></script><scripttype="text/Javascript">$(document).ready(function(){

$("button").click(function(){

$("div").remove("#first");

})

})</script></head><body><divid="first">我是第一</div><divid="second">我是第二</div><button>点击</button></body></html>

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

<!DOCTYPE html><html><head><metacharset="utf-8"><metaname="author"content="http://www.51texiao.cn/"/><title>蚂蚁部落</title><scripttype="text/javascript"src="mytest/jQuery/jquery-1.8.3.js"></script><scripttype="text/javascript">$(document).ready(function(){

$("#btd").click(function(){

$("div").remove();

})

})</script></head><body><divid="first">我是第一</div><divid="second">我是第二</div><buttonid="btd">点击删除第一个div</button></body></html>

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

<!DOCTYPE html><html><head><metacharset="utf-8"><metaname="author"content="http://www.51texiao.cn/"/><title>蚂蚁部落</title><styletype="text/CSS">div{width:200px;height:200px;border:5px solid green;}</style><scripttype="text/javascript"src="mytest/jQuery/jquery-1.8.3.js"></script><scripttype="text/javascript">$(document).ready(function(){

$("#btd").click(function(){vara=$("div");

a.remove("#first");

$("#btn").click(function(){

alert(a.length);

})

})

})</script></head><body><divid="first">我是第一</div><divid="second">我是第二</div><buttonid="btd">删除第一个div</button><buttonid="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

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
© 2005- 王朝网络 版权所有