jquery中可以使用each()方法实现循环,each() 方法规定为每个匹配元素规定运行的函数。返回 false 可用于及早停止循环。
语法$(selector).each(function(index,element))
参数:
function(index,element):必需。为每个匹配元素规定运行的函数。index - 选择器的 index 位置
element - 当前的元素(也可使用 "this" 选择器)
示例:
$(document).ready(function(){
$("button").click(function(){
$("li").each(function(){
alert($(this).text())
});
});
});
输出每个列表项的值
- Coffee
- Milk
- Soda
更多相关知识请关注web前端课程