使用each方法代替for遍历数组

纯粹是为了写这个好玩

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<script src="jquery-1.4.4.js" type="text/javascript"></script>
<script type="text/javascript">
var array=["huangbiao","24","boy"];
var length = array.length;

function forFunc(){
	var result = "";
	for(var i = 0; i < length; i++){
		result=result.concat(array[i]+" ; ");
	}
	$("#forFunc").html(result);
	return true;
}

function eachFunc(){
	var result = "";
	$(array).each(function(i,n){
	//下面两种方式实现的效果是一样的
		//result=result.concat(array[i]+" ; ");
		result=result.concat(n+" ; ");
	});
	$("#eachFunc").html(result);
}

</script>
<title>无标题文档</title>
</head>
<body>
	<button onclick="forFunc()">forFunc</button><br>
	<div id="forFunc"></div>
	<button onclick="eachFunc()">eachFunc</button><br>
	<div id="eachFunc"></div>
</body>
</html>

 

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭