微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

jquery怎么清除兄弟元素

jquery清除兄弟元素的方法:1、使用siblings()方法获取被选元素的所有兄弟元素;2、使用remove()方法删除获取到的兄弟元素,语法“$(selector).siblings().remove()”。

本教程操作环境:windows7系统、jquery1.10.2版本、Dell G3电脑。

jquery清除兄弟元素的方法

<!DOCTYPE html>
<html>
	<head>
		<Meta charset=utf-8>
		<style>
		.siblings,.siblings *{ 
		display: block;
		border: 2px solid lightgrey;
		color: lightgrey;
		padding: 5px;
		margin: 15px;
		}
		</style>
		<script src=js/jquery-1.10.2.min.js></script>
		<script type=text/javascript>
			$(document).ready(function() {
				$(button).click(function() {
					$(h2).siblings().remove();
				});
			});
		</script>
	</head>
	<body>
		<div class=siblings>div (父)
			<p>p(兄弟元素)</p>
			<span>span(兄弟元素)</span>
			<h2>h2(本元素)</h2>
			<h3>h3(兄弟元素)</h3>
			<p>p(兄弟元素)</p>
		</div>
		<button>清除兄弟元素</button>
	</body>
</html>

1.gif

相关视频教程推荐:jQuery教程(视频)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐