jquery中each的使用形式是“$(selector).each(function(index,element))”,each()方法可以规定匹配元素运行的函数,常用于遍历指定的对象和数组。
本教程操作环境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>