博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
jquery中each_如何在jQuery中使用each()
阅读量:2533 次
发布时间:2019-05-11

本文共 2575 字,大约阅读时间需要 8 分钟。

jquery中each

In this post, we are going to discuss about jQuery each() method, which is one of the most important and mostly used traversing functions in jQuery.

在本文中,我们将讨论jQuery each()方法,这是jQuery中最重要且使用最广泛的遍历函数之一。

jQuery each() (jQuery each())

This method lets you to iterate over the DOM elements of the jQuery object and executes a function for each matched element.

You have to pass a callback function to each() method which is executed for each selected element. The call back function is triggered in the context of the current DOM element, so you can use this keyword to refer the currently matched element. You can return false from the callback function to stop the loop early.

该方法使您可以迭代jQuery对象的DOM元素,并为每个匹配的元素执行一个函数。

您必须将回调函数传递给each()方法,该方法针对每个选定的元素执行。 回调函数是在当前DOM元素的上下文中触发的,因此您可以使用this关键字来引用当前匹配的元素。 您可以从回调函数返回false,以尽早停止循环。

Here is the general syntax for using jQuery each() method:

这是使用jQuery each()方法的一般语法:

  • selector.each(function)

    选择器。每个(功能)

function is the call back method triggered for each matched element.This is a mandatory parameter for the each() method. function(index,element) takes two parameters , index and element.

index an integer value, which is the index of the selected element.
element is the selected element itself.

function是为每个匹配的元素触发的回调方法。这是each()方法的必需参数。 function(index,element)具有两个参数indexelement

index一个整数值,它是所选元素的索引。
element是所选元素本身。

jQuery each()示例 (jQuery each() example)

Following example demonstrates the jQuery each() usage.

以下示例演示了jQuery each()的用法。

jQuery Traversing each
Stop

In this example, the each() is triggered on clicking the button. The each() takes a function(index, element) as an argument. The each() iterates over the selected div elements starting from the first index, that is from index 0. The function is executed for each selected div and changes the CSS style of the currently matched div.

在此示例中,单击按钮会触发each() 。 each()采用一个function(index, element)作为参数。 each()从第一个索引(即索引0)开始迭代选定的div元素。该函数针对每个选定的div执行,并更改当前匹配的divCSS样式。

This iteration stops when the function returns false. Here, is() method checks for an “id=stop” and the passed function returns false when the index is 4, which in turn stops the iteration.

函数返回false时,此迭代停止。 在这里,is()方法检查“ id=stop ”,并且当索引为4时,传递的函数返回false,这又停止了迭代。

You can click on below button to try it out yourself.

您可以单击下面的按钮尝试一下。

I hope the jQuery each() usage is clear now. That’s all for now. We will discuss some more traversing techniques in the coming posts.

我希望jQuery each()用法现在已经清楚了。 目前为止就这样了。 我们将在以后的文章中讨论更多的遍历技术。

翻译自:

jquery中each

转载地址:http://xdlzd.baihongyu.com/

你可能感兴趣的文章
python3 提成计算
查看>>
VBA赋值给指定单元格
查看>>
抽象类和接口总结回顾
查看>>
【语言处理与Python】5.3使用Python字典映射词及其属性
查看>>
设备信息
查看>>
Android Volley框架的使用(三)
查看>>
[错误总结] 控件frame设置无效
查看>>
Redis Java API
查看>>
oracle 查询表的定义语句
查看>>
Android 笔记之 Android 系统架构
查看>>
状压dp终极篇(状态转移的思想)
查看>>
AtCoder Grand Contest 031 B - Reversi
查看>>
完整成功配置wamp server小记
查看>>
build.gradle添加Oracle jdbc6 链接
查看>>
影响系统性能的20个瓶颈--转自开源中国
查看>>
根据ISBN获取豆瓣API提供的图书信息
查看>>
【转】Python中*args和**kwargs的区别
查看>>
git命令简单使用
查看>>
CODEFORCES 125E MST Company 巧用Kruskal算法
查看>>
C++标准库分析总结(三)——<迭代器设计原则>
查看>>