首頁  >  文章  >  web前端  >  jquery怎麼清除兄弟元素

jquery怎麼清除兄弟元素

青灯夜游
青灯夜游原創
2021-11-19 11:45:113197瀏覽

jquery清除兄弟元素的方法:1、使用siblings()方法取得被選元素的所有兄弟元素;2、使用remove()方法刪除取得到的兄弟元素,語法「$(selector). siblings().remove()」。

jquery怎麼清除兄弟元素

本教學操作環境:windows7系統、jquery1.10.2版本、Dell G3電腦。

jquery清除兄弟元素的方法

  • #使用siblings()方法來取得所有被選元素的兄弟元素

  • 使用remove()方法刪除取得到的兄弟元素

<!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>

jquery怎麼清除兄弟元素

相關影片教學推薦:jQuery教學(影片)

以上是jquery怎麼清除兄弟元素的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn