亚洲最大看欧美片,亚洲图揄拍自拍另类图片,欧美精品v国产精品v呦,日本在线精品视频免费

  • 站長資訊網(wǎng)
    最全最豐富的資訊網(wǎng)站

    jquery怎么判斷一個元素是否是另一個元素的子元素

    兩種方法:1、使用children()和length屬性進(jìn)行判斷,語法“指定元素對象.children(指定子元素對象).length==0”,如果返回值為真則不存在,反之則存在。2、使用find()和length屬性進(jìn)行判斷,語法“指定元素對象.find(指定子元素對象).length==0”,如果返回值為真則不存在,反之則存在。

    jquery怎么判斷一個元素是否是另一個元素的子元素

    前端(vue)入門到精通課程:進(jìn)入學(xué)習(xí)
    Apipost = Postman + Swagger + Mock + Jmeter 超好用的API調(diào)試工具:點(diǎn)擊使用

    本教程操作環(huán)境:windows7系統(tǒng)、jquery3.6.1版本、Dell G3電腦。

    判斷一個元素是否是另一個元素的子元素,就是判斷一個元素的子元素是否是另一個指定元素。

    在jquery中,想要查找子元素有兩種方法:

    • children()方法:獲取該元素下的直接子集元素

    • find()方法:獲取該元素下的所有(包括子集的子集)子集元素

    因此利用這兩個方法可以判斷一個元素是否是另一個元素的子元素。

    方法1:使用children()和length屬性進(jìn)行判斷

    children() 方法返回返回被選元素的所有直接子元素。

    用于判斷指定元素A中是否存在指定子元素B的語法:

    A.children(B).length==0
    登錄后復(fù)制

    • A.children(B),會返回A元素的所有直接子元素B對象集合

    • 對象集合.length==0,判斷對象集合是否為0,如果為0則不存在,反之則存在

    示例:

    <!DOCTYPE html> <html> 	<head> 		<meta charset="utf-8"> 		<script src="js/jquery-3.6.1.min.js"></script> 		<style> 			div * { 				display: block; 				border: 2px solid lightgrey; 				color: lightgrey; 				padding: 5px; 				margin: 15px; 			} 		</style>  		<script> 			$(document).ready(function() { 				$("button").on("click", function() { 					$("ul").children("#box").css({ 						"color": "red", 						"border": "2px solid red" 					}); 					var number =$("ul").children("#box").length; 					console.log(number); 					if (number == 0) { 						  console.log("不含"); 					}else{ 					     console.log("含有"); 					} 				}); 			}); 		</script> 	</head>  	<body class="ancestors"> 		<div style="width:500px;">div (父節(jié)點(diǎn)) 			<ul>ul (指定元素) 				<li id="box">li (子節(jié)點(diǎn)1) 					<span>span (孫節(jié)點(diǎn)1)</span> 				</li> 				<li>li (子節(jié)點(diǎn)2) 					<span>span (孫節(jié)點(diǎn)2)</span> 				</li> 				<li>li (子節(jié)點(diǎn)3) 					<span>span (孫節(jié)點(diǎn)3)</span> 				</li> 			</ul> 		</div> 		<button>選取ul的所有子元素#box</button> 	</body>  </html>
    登錄后復(fù)制

    jquery怎么判斷一個元素是否是另一個元素的子元素

    方法2:使用find()和length屬性進(jìn)行判斷

    find() 方法獲得當(dāng)前元素集合中每個元素的后代,通過選擇器、jQuery 對象或元素來篩選。

    判斷語法:

    A.find(B).length==0
    登錄后復(fù)制

    語法意思其實(shí)和方法1類似,可參考。

    示例:

    <script> 	$(document).ready(function() { 		$("button").on("click", function() { 			$("ul").find("#box").css({ 				"color": "red", 				"border": "2px solid red" 			}); 			var number =$("ul").find("#box").length; 			console.log(number); 			if (number == 0) { 				  console.log("不含"); 			}else{ 				 console.log("含有"); 			} 		}); 	}); </script>
    登錄后復(fù)制

    jquery怎么判斷一個元素是否是另一個元素的子元素

    【推薦學(xué)習(xí):jQuery視頻教程、web前端視頻】

    贊(0)
    分享到: 更多 (0)
    網(wǎng)站地圖   滬ICP備18035694號-2    滬公網(wǎng)安備31011702889846號