<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<!-- 遍历 -->
<script type="text/javascript" src="<c:url value='/js/jquery-3.3.1.js'/>"></script>
<script type="text/javascript">
$(function(){
/* 为each按钮添加一个点击事件 */
//遍历方式1
$("#each").click(function(){
$("div").each(function(i,val){
alert(i+","+$(val).html() );
});
});
//遍历方式2
$("#each2").click(fn_each);
});
function fn_each(){
var arr = [1,3,1,5];
$.each(arr,function(i,val){
alert(i+","+val );
});
}
</script>
</head>
<body>
<button id="each">遍历下面的div,拿到里面的值</button><br>
<button id="each2">遍历一个数组</button>
<div>111</div>
<div>222</div>
<div>333</div>
</body>
</html>