jQuery `parent > child`選擇器


jQuery 中的 parent > child 選擇器用於選擇作為指定元素直接子級的全部元素。

語法

語法如下 −

("parent > child")

示例

讓我們來看一個實現 jQuery parent > child selector 的示例 −

 即時演示

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
   $(document).ready(function(){
      $("div > span").css("color", "red");
   });
</script>
<style>
div {
   border:1px solid black;
   padding:10px;
}
</style>
</head>
<body>
<h2>Demo Heading</h2>
<div>
<p>This is demo text.</p>
<span>span outside p element</span>
<p>This is demo text.</p>
</div><br>
<div>
<p>This is demo text.</p>
<p>This is demo text. <span>span inside p element.</span></p>
<p>This is demo text.</p>
</div>
</body>
</html>

輸出

這會產生以下輸出 −

更新於: 2019-12-31

915 次瀏覽

開啟你的職業

透過完成課程取得認證

開始
廣告
© . All rights reserved.