jQuery children() 方法
JQuery 中的 children() 方法用於返回所選元素的所有直接子元素。
示例
讓我們看一個使用 jQuery children() 方法的示例:
<!DOCTYPE html> <html> <head> <style> .demo * { display: block; border: 2px solid blue; padding: 10px; margin: 25px; } .one { border: 2px solid yellow; } </style> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("div").children().addClass("one"); }); </script> </head> <body> <h1>Heading One</h1> <div class="demo" style="width:600px;">This is our demo text in div. <p>child <span>grandchild</span> <span>grandchild</span> </p> <p>child <span>grandchild</span> </p> </div> </body> </html>
輸出
這將產生以下輸出:
示例
現在我們來看另一個示例:
<!DOCTYPE html> <html> <head> <style> .demo * { display: block; border: 3px dashed red; padding: 10px; margin: 25px; } .one { border: 2px solid yellow; } </style> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("div").children().addClass("one"); }); </script> </head> <body> <h1>Heading One</h1> <div class="demo" style="width:600px;">This is our demo text in div. <p>child <span>grandchild</span> <span>grandchild</span> </p> <p>child <span>grandchild</span> <span>grandchild</span> </p> </div> </body> </html>
輸出
這將產生以下輸出:
廣告