---
title: " $(document.body).|.each(false)|this.style.color|$(this).toggleClass(\"example\")"
author: "mssp"
date: "2013-12-10T16:58:10Z"
tags: []
categories: ["Web前端"]
slug: "document-body-each-false-this-style-color-this-toggleclass-example"
draft: false
lastmod: "2013-12-10T17:02:33Z"
id: 1987807
url: "https://mssp.iteye.com/blog/1987807"
---

```javascript


<!DOCTYPE html>
<html>
<head>
  <style>
  div { color:red; text-align:center; cursor:pointer; 
        font-weight:bolder; width:300px; }
  </style>
  <script src="http://code.jquery.com/jquery-latest.min.js"></script>
</head>
<body>
  <div>Click here</div>

  <div>to iterate through</div>
  <div>these divs.</div>
<script>
    $(document.body).click(function () {
      $("div").each(function (i) {
        if (this.style.color != "blue") {
          this.style.color = "blue";
        } else {
          this.style.color = "";
        }
      });
    });</script>

</body>
</html>


```

<span style="color:red">      if ($(this).is("#stop")) {
          $("span").text("Stopped at div index #" + index);
          return false;
        }</span>


```javascript

<script>
    $("button").click(function () {
      $("div").each(function (index, domEle) {
        // domEle == this
        $(domEle).css("backgroundColor", "yellow"); 
        if ($(this).is("#stop")) {
          $("span").text("Stopped at div index #" + index);
          return false;
        }
      });
    });

</script>



```