返回介绍

.each()

发布于 2017-09-11 14:04:24 字数 4212 浏览 1128 评论 0 收藏 0

所属分类:杂项 > 集合操作 | 遍历

.each( function(index, Element) )返回: jQuery

描述: 遍历一个jQuery对象,为每个匹配元素执行一个函数。

  • 添加的版本: 1.0.each( function(index, Element) )

    • function(index, Element) 类型: Function() 为每个匹配元素执行的一个函数。

.each() 方法用来让DOM循环结构更简单更不易出错。它会迭代jQuery对象中的每一个DOM元素。每次回调函数执行时,会传递当前循环次数作为参数(从0开始计数)。更重要的是,回调函数是在当前DOM元素为上下文的语境中触发的。因此关键字 this 总是指向这个元素。

假设页面上有这样一个简单的无序列表。

<ul>
    <li>foo</li>
    <li>bar</li>
</ul>

你可以选中并迭代这些列表:

$( "li" ).each(function( index ) {
  console.log( index + ": "" + $(this).text() );
});

列表中每一项会显示在下面的消息中:

0: foo
1: bar

我们可以通过返回 false以便在回调函数内中止循环。

注意: jQuery的方法,返回一个jQuery对象遍历jQuery集合中的元素 - 被称为隐式迭代的过程。当这种情况发生时,它通常不需要显式地循环的.each()方法:

// The .each() method is unnecessary here:
$( "li" ).each(function() {
  $(this).addClass( "foo" );
});
 
// Instead, you should rely on implicit iteration:
$( "li" ).addClass( "bar" );

例子:

Example: 遍历三个div并设置它们的color属性。

<!DOCTYPE html>
<html>
<head>
  <style>
  div { color:red; text-align:center; cursor:pointer;
        font-weight:bolder; width:300px; }
  </style>
  <script src="http://cdn.bootcss.com/jquery/1.11.2/jquery.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>

Example: 如果你不想要普通的DOM元素,而想获得的是jQuery对象的话,使用$(this)函数。例如:

<!DOCTYPE html>
<html>
<head>
  <style>
  ul { font-size:18px; margin:0; }
  span { color:blue; text-decoration:underline; cursor:pointer; }
  .example { font-style:italic; }
  </style>
  <script src="http://cdn.bootcss.com/jquery/1.11.2/jquery.min.js"></script>
</head>
<body>
  To do list: <span>(click here to change)</span>
  <ul>
    <li>Eat</li>
    <li>Sleep</li>
 
    <li>Be merry</li>
  </ul>
<script>
    $( "span" ).click(function () {
      $( "li" ).each(function(){
        $( this ).toggleClass( "example" );
      });
    });
 
</script>
 
</body>
</html>

Example: 你可以使用 'return' 来提前结束 each() 循环。

<!DOCTYPE html>
<html>
<head>
  <style>
  div { width:40px; height:40px; margin:5px; float:left;
        border:2px blue solid; text-align:center; }
  span { color:red; }
  </style>
  <script src="http://cdn.bootcss.com/jquery/1.11.2/jquery.min.js"></script>
</head>
<body>
  <button>Change colors</button>
  <span></span>
  <div></div>
  <div></div>
 
  <div></div>
  <div></div>
  <div id="stop">Stop here</div>
  <div></div>
 
  <div></div>
  <div></div>
<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>
 
</body>
</html>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文