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>
输出结果:
点击按钮后输出如下:
相关视频教程推荐:jQuery视频教程
以上就是jquery中each的使用形式是什么的详细内容,转载自php中文网
发表评论 取消回复