<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP ‘test3.jsp‘ starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script src="js/jquery-2.1.1.min.js"></script>
<script type="application/javascript">
//遍历数组的每一项,只适合遍历一维数组
// var array=[1,2,3,[4,[5]]];
// array.forEach(function(item,index,array){
// alert(item);
// });
//自己实现一个能遍历多维数组的Array each方法
var array2=[1,2,3,[4,[5,[6]]]];
Array.prototype.each=function(fn){
try{
//1.遍历数组的每一项
this.i || (this.i=0);
//严谨的判断,什么时候进入条件
//当数组的长度大于0
if(this.length>0 && fn.constructor==Function){
while(this.i<this.length){ //循环遍历数组的每一项
//获取数组的每一项
var e= this[this.i];
//如果胡取到当前元素并且当前元素是数组,递归
if(e && e.constructor==Array){
e.each(fn);
}else{ //获取到当前元素是单个元素
//这的目的就是执行传递的函数并且把数组的当前元素传递给函数让函数执行
//fn.apply(e,[e]);
fn.call(e,e);
}
this.i++;
}
this.i=null; //使用完i回收垃圾
}
}catch(ex){
alert("出错");
}
return this;
}
array2.each(function(item){
alert(item);
});
</script>
</head>
<body>
This is my JSP page. <br>
</body>
</html>本文出自 “matengbing” 博客,请务必保留此出处http://matengbing.blog.51cto.com/11395502/1879311
原文:http://matengbing.blog.51cto.com/11395502/1879311