1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    7. <title>Document</title>
    8. </head>
    9. <body>
    10. <div class="box">
    11. <div>
    12. <div class="aaa">
    13. <p class="aaa">
    14. <span>后代元素</span>
    15. </p>
    16. </div>
    17. </div>
    18. </div>
    19. <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    20. <script>
    21. var $span = $('span');
    22. // 查找所有的祖宗元素
    23. var $parents = $span.parents();
    24. console.log($parents);
    25. // 查找祖宗元素里离span最近的的类名为aaa的元素
    26. var $aaa = $span.closest('.aaa');
    27. console.log($aaa);
    28. </script>
    29. </body>
    30. </html>