1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
<%@ 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 51CTO博客,原文链接:http://blog.51cto.com/matengbing/1879311