jQuery中remove()方法用法实例
本文实例讲述了jQuery中remove()方法用法。分享给大家供大家参考。具体分析如下:
此方法将会从DOM中删除所有匹配的元素。
说明:remove()方法不会把匹配的元素从jQuery对象中删除,因而可以在将来再使用这些匹配的元素,不过除了这个元素本身得以保留之外,其他的比如绑定的事件,附加的数据等都会被移除。
语法结构:
$(selector).remove(expr)
参数列表:
实例一:
<!DOCTYPEhtml>
<html>
<head>
<metacharset="utf-8">
<metaname="author"content="https://www.nhooo.com/"/>
<title>remove()函数-毛票票</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。
实例二:
<!DOCTYPEhtml>
<html>
<head>
<metacharset="utf-8">
<metaname="author"content="https://www.nhooo.com/"/>
<title>remove()函数-毛票票</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>
如果省略参数,那就是删除所有匹配的元素。
实例三:
<!DOCTYPEhtml>
<html>
<head>
<metacharset="utf-8">
<metaname="author"content="https://www.nhooo.com/"/>
<title>remove()函数-毛票票</title>
<styletype="text/css">
div{
width:200px;
height:200px;
border:5pxsolidgreen;
}
</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对象中删除。
希望本文所述对大家的jQuery程序设计有所帮助。
热门推荐
10 前程祝福语简短暖心
11 简短祝福语蛋糕简笔画
12 生日祝福语简短搞笑女儿
13 老师节祝福语的简短
14 经典祝福语简短情侣句子
15 生日祝福语古语简短创意
16 入学校简短祝福语
17 年底拜年祝福语大全简短
18 写生日祝福语简短唯美