andSelf()方法将先前的选择添加到当前选择中。
当脚本中有多个遍历,然后添加在上一个遍历之前匹配的内容时,该方法很有用。
selector.andSelf( )
以下是一个简单的示例,简单说明了此方法的用法-
<html> <head> <title>The jQuery Example</title> <script type="text/javascript" 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>
在这里,边框将被添加到先前的选择中,这是一个划分,然后是第二个选择,其是段落,如下所示-
First Paragraph
Second Paragraph
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)
Tony Bai · Go语言第一课 -〔Tony Bai〕
PPT设计进阶 · 从基础操作到高级创意 -〔李金宝(Bobbie)〕