var result = []
for (var i = 0 i < this.length i++) {
var arr = this[i]
var sum = 0 // a数组总和
for (var j = 0 j < arr.length j++) {
sum += arr[j]
}
result.push(sum / arr.length)
}
return result // 返回每行的平均值
}
$(function () {
var arr = [[1, 2, 3, 4], [2, 3, 1, 5], [4, 5, 2, 6], [5, 6, 7, 1]] // 定义二维数组
var result = arr.row_averages() // 调用
var str = ''
for (var i = 0 i < result.length i++) {
str += ' 平均值=' + result[i] + '<br/>'
}
$('#div').html(str)
})
<div id="div"></div>
如下代码:
<!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=utf8" />
<title>demo</title>
<script type="text/javascript">
var count = 0 //人数
//写 Cookie
function setCookie(name,value) {
var Days = 30
var exp = new Date()
exp.setTime(exp.getTime() + Days*24*60*60*1000)
document.cookie = name + "="+ escape (value) + "expires=" + exp.toGMTString()
}
//读取 cookie
function getCookie(name) {
var arr,reg=new RegExp("(^| )"+name+"=([^]*)(|$)")
if(arr=document.cookie.match(reg)) {
return unescape(arr[2])
} else {
return null
}
}
//人数+1
function increase() {
setCookie("count", ++count)
var msg = document.getElementById("msg")
msg.innerText = "人数:" + count
}
//人数-1
function decrease() {
setCookie("count", --count)
var msg = document.getElementById("msg")
msg.innerText = "人数:" + count
}
window.onload = function() {
//页面加载时从cookie加载人数
count = getCookie("cookie")
if(count == null) {
count = 0
}
var msg = document.getElementById("msg")
msg.innerText = "人数:" + count
}
</script>
</head>
<body>
<span id="msg">人数:0</span>
<button onclick="increase()">人数+1</button>
<button onclick="decrease()">人数-1</button>
</body>
</html>
</html>
只放到Web服务器上时,Cookie才会生效。