jQuery.andSelf( ) 方法在 jQuery 中有何作用?
andSelf( ) 方法將之前的選擇新增到當前選擇中。當指令碼有多個遍歷,然後新增之前遍歷匹配的內容時,該方法很有用。
示例
你可以嘗試執行以下程式碼以學習如何使用 jQuery.andSelf() 方法
<html> <head> <title>jQuery andSelf() method</title> <script src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> <script> $(document).ready(function(){ $("div").find("p").andSelf().addClass("border"); }); </script> <style> p, div { margin:5px; padding:5px; } .border { border: 2px solid red; } .background { background:yellow; } </style> </head> <body> <div> <p>First Paragraph</p> <p>Second Paragraph</p> </div> </body> </html>
廣告