jQuery的remove()方法使用详解 - Web前端
作者:98wpeu发布时间:2026-08-20分类:网页前端技术浏览:9
导读:remove()方法的定义和用法:此方法将会从DOM中删除所有匹配的元素。说明:remove()方法不会把匹配的元素从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>
看到这段代码之后,我想不用我过多的解释了。大家就明白了吧,很有意思的方法。
相关推荐
- jQuery+Ajax+PHP+Mysql实现分页显示数据实例讲解 - Web前端
- JQuery+Ajax实现数据查询、排序和分页功能 - Web前端
- jQuery实现带分组数据的Table表头排序实例分析 - Web前端
- jQuery实现单击弹出Div层窗口效果(可关闭可拖动) - Web前端
- 基于Jquery+div+css实现弹出登录窗口(代码超简单) - Web前端
- jQuery实现多级下拉菜单jDropMenu的方法 - Web前端
- jQuery表单验证功能实例 - Web前端
- jQuery 如何给Carousel插件添加新的功能 - Web前端
- jQuery实现点击小图片淡入淡出显示大图片特效 - Web前端
- jquery实现在网页指定区域显示自定义右键菜单效果 - Web前端
- 网页前端技术排行
-
- 1[Web前端]用javascript实现默认图片替代未显示的图片 - Web前端
- 2分析Iconfont-阿里巴巴矢量常用图标库 - Web前端
- 3jQuery实例教程:制作网页中可折叠的面板 - Web前端
- 4【第六章】Foundation之按钮和下拉功能 - Web前端
- 5基于jquery的滚动条滚动固定div(附演示下载) - Web前端
- 6jQuery编写widget的一些技巧分享 - Web前端
- 7分享精心挑选的12款优秀jQuery Ajax分页插件和教程 - Web前端
- 8jQuery实现鼠标移到元素上动态提示消息框效果 - Web前端
- 9在Mac/PC上远程调试iPhone/iPad上的网页 - Web前端
- 最近发表
-
- WordPress随机显示特色图片插件:Random Post Thumbnails
- KeePass实现Chrome浏览器自动填充密码方法一
- LNMP一键包nginx 301强制跳转到https教程
- KeePass实现Chrome浏览器自动填充密码方法二
- #建站# 免费的VPS管理软件Xshell8/Xftp8中文版下载
- 使用Xshell 8连接VPS教程_电脑登录vps的方法
- WordPress评论界面添加烟花????效果
- 不同浏览器书签同步方案:坚果云+Floccus_详细使用教程
- iOS端KeePassXC客户端APP:Strongbox Password Safe
- 给WordPress评论中的Gravatar头像图片添加ALT属性


