jquery中each的使用形式是什么

新资讯 2022-04-28 14:35:29 浏览 买帖修改

jquery中each的使用形式是“$(selector).each(function(index,element))”,each()方法可以规定匹配元素运行的函数,常用于遍历指定的对象和数组。

tG907。WuhAnEwS.cn

本教程操作环境:windows10系统、jquery3.2.1版本、Dell G3电脑。

jquery中each的使用形式是什么

each() 方法规定为每个匹配元素规定运行的函数。

提示:返回 false 可用于及早停止循环。

语法如下:

$(selector).each(function(index,element))

其中参数表示如下:

function(index,element)是必需参数,表示为每个匹配元素规定运行的函数。

index表示选择器的 index 位置,element表示当前的元素(也可使用 "this" 选择器)

示例如下

<html><head><script type="text/javascript" src="//jquery/jquery.js"></script><script type="text/javascript">$(document).ready(function(){ $("button").click(function(){ $("li").each(function(){ alert($(this).text()) }); });});</script></head><body><button>输出每个列表项的值</button><ul><li>Coffee</li><li>Milk</li><li>Soda</li></ul></body></html>