要从jQuery的父级中选择所有子级,请使用find()
方法。您可以尝试运行以下代码以选择任何级别的所有子代-
<!DOCTYPE html> <html> <head> <style> .myclass * { display: block; border: 2px solid lightgrey; color: lightgrey; padding: 5px; margin: 15px; } </style> <script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script> <script> $(document).ready(function(){ $('ul').find('*').css({"color": "red", "border": "2px solid green"}); }); </script> </head> <body> <div class="myclass"> <div style="width:400px;">div - great-grandparent <ul>ul <li>li- child - level 1 <ul>ul- child - level 2 <li>li- child - level 3</li> <li>li- child - level 3</li> </ul> </li> </ul> </div> </div> </body> </html>