当前位置: 移动技术网 > IT编程>开发语言>JavaScript > jQuery中remove()方法用法实例教程

jQuery中remove()方法用法实例教程

2019年03月28日  | 移动技术网IT编程  | 我要评论

本文实例讲述了jquery中remove()方法用法。分享给大家供大家参考。具体分析如下:

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

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

语法结构:

代码如下:

$(selector).remove(expr)

 

参数列表:

参数 描述
expr 可选。用于筛选元素的jquery表达式
实例代码:

 

实例一:

 

代码如下:


<!doctype html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="https://www.cnblogs.com/" />
<title>remove()函数-博客园</title>
<script type="text/javascript" src="mytest/jquery/jquery-1.8.3.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $("button").click(function(){
    $("p").remove("#first");
  })
})
</script>
</head>
<body>
<p id="first">我是第一</p>
<p id="second">我是第二</p>
<button>点击</button>
</body>
</html>

 

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

实例二:

 

代码如下:


<!doctype html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="https://www.cnblogs.com/" />
<title>remove()函数-博客园</title>
<script type="text/javascript" src="mytest/jquery/jquery-1.8.3.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $("#btd").click(function(){
    $("p").remove();
  })
})
</script>
</head>
<body>
<p id="first">我是第一</p>
<p id="second">我是第二</p>
<button id="btd">点击删除第一个p</button>
</body>
</html>

 

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

实例三:

 

代码如下:


<!doctype html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="https://www.cnblogs.com/" />
<title>remove()函数-博客园</title>
<style type="text/css">
p{
  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=$("p");
    a.remove("#first");
    $("#btn").click(function(){
      alert(a.length);
    })
  })
})
</script>
</head>
<body>
<p id="first">我是第一</p>
<p id="second">我是第二</p>
<button id="btd">删除第一个p</button><button id="btn">查看删除操作后p的数量</button>
</body>
</html>

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网