jQuery 中 jQuery.children() 方法有什麼用?


如果你想返回選定元素的所有直接子元素,那麼使用 jQuery.children() 方法,

示例

你可以嘗試執行以下程式碼來了解如何在 jQuery 中使用 jQuery.children() 方法,

線上演示

<!DOCTYPE html>
<html>
<head>
<style>
.myclass * {
    display: block;
    border: 2px solid blue;
    color: black;
    padding: 3px;
    margin: 10px;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("ol").children().css({"color": "red", "border": "5px solid red"});
});
</script>
</head>

<body class="myclass">body
  <div style="width:600px;">div
    <ol>ol
      <li>li - This is the child
        <span>span</span>
      </li>
    </ol>  
  </div>
</body>

</html>

更新於: 09-Dec-2019

113 次瀏覽

開啟你的職業生涯

透過完成課程獲得認證

開始學習
廣告