如何在 jQuery 中查找特定类型元素的后代元素?

jqueryweb developmentfront end technology

使用 jQuery 的 find() 方法查找特定类型元素的后代元素。jQuery.find() 方法会返回所选元素的后代元素。

示例

您可以尝试运行以下代码来学习如何使用 jQuery.find() 方法。

<!DOCTYPE html>
<html>
<head>
<style>
 .myclass * {
    display: block;
    border: 2px solid red;
    padding: 2px;
    margin: 10px;
    color: red;
 }
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
  $(document).ready(function(){
    $("ol").find("span").css({"background-color": "blue", "border": "5px solid gray"});
  });
</script>
</head>
<body class="myclass">body
  <div style="width:500px;">div
    <ol>ol  
      <li>li
        <span>The descendant element</span>
      </li>
    </ol>  
  </div>
</body>
</html>

相关文章