---
title: "jquery 遍历 andSelf()"
author: "KAXU"
date: "2009-01-08T17:10:54Z"
tags: ["HTML", "jQuery"]
slug: "jquery-andself"
draft: false
lastmod: "2009-01-08T17:10:54Z"
id: 312559
url: "https://kaxu.iteye.com/blog/312559"
---

andSelf()

将之前匹配元素集合增加到当前匹配元素集合中。返回匹配元素集合

```
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
                    "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
  
  <script>
  $(document).ready(function(){
    
    $("div").find("p").andSelf().addClass("border");
    $("div").find("p").addClass("background");

  });
  </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>
```

 

$("div").find("p").andSelf().addClass("border");  
将匹配div元素集合增加到匹配p元素集合中。