jQuery的remove()方法使用详解
网络编程 2021-07-04 21:48www.168986.cn编程入门
从DOM中删除所有匹配的元素。这个方法不会把匹配的元素从jQuery对象中删除,因而可以在将来再使用这些匹配的元素。但除了这个元素本身得以保留之外,其他的比如绑定的事件,附加的数据等都会被移除。我的理解就是将元素移除了。
remove()方法的定义和用法:
此方法将会从DOM中删除所有匹配的元素。
说明:remove()方法不会把匹配的元素从jQuery对象中删除,因而可以在将来再使用这些匹配的元素,不过除了这个元素本身得以保留之外,其他的比如绑定的事件,附加的数据等都会被移除。
语法结构:
$(selector).remove(expr)
参数列表:
参数 描述
expr 可选。用于筛选元素的jQuery表达式
实例代码:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script type="text/javascript" src="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"> <script type="text/javascript" src="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"> <style type="text/css"> div{ width:200px; height:200px; border:5px solid green; } </style> <script type="text/javascript" src="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对象中删除。
jquery使用remove()方法删除指定class子元素的方法
<!DOCTYPE html> <html> <head> <script src="js/jquery.min.js"> </script> <script> $(document).ready(function(){ $("button").click(function(){ $("p").remove(".italic"); }); }); </script> </head> <body> <p>This is a paragraph in the div.</p> <p class="italic"><i>This is another paragraph in the div.</i></p> <p class="italic"><i>This is another paragraph in the div.</i></p> <button>Remove all p elements with class="italic"</button> </body> </html>
看到这段代码之后,我想不用我过多的解释了。大家就明白了吧,很有意思的方法。
编程语言
- 机械手焊接机器人编程 机械手焊接设备编程
- vb数据库编程实例,vb做数据库
- tiobe世界编程语言排行榜,全球编程语言排行
- 成都java编程培训 成都it编程培训中心
- 编程基础培训,编程基础知识培训
- flash游戏编程基础教程,flash游戏开发教程
- 不会英语能学编程吗 不会英语可以学程序员吗
- 爱心代码编程C语言 爱心代码编程c语言公式
- c语言编程学习入门 c语言编程快速入门
- 西门子plc编程培训 西门子plc编程培训班
- 哪里可以学编程 台州哪里可以学编程
- 少儿编程哪个好 少儿编程哪个品牌好
- 编程是什么意思,图形编程是什么意思
- c语言编程软件下载,c语言编程软件在哪下载
- java编程软件下载,java编程下载手机版
- ug编程培训学校,ug编程培训教程