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>
使用的angular.js来实现,具体代码如下:
<!DOCTYPE html><html>
<head>
<meta charset="utf-8">
<script src="https://cdn.bootcss.com/angular.js/1.4.6/angular.min.js"></script>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
</head>
<body>
<div ng-app="myApp" ng-controller="myCtrl">
<input type="text" ng-model="aNum" ng-keyup="getResult()">+
<input type="text" ng-model="bNum" ng-keyup="getResult()">-
<input type="基羡text" ng-model="cNum" ng-keyup="getResult()">=
<div id="result" style="display:inline" ng-model="result">{{result||'搏盯拍null'}}</div>
</div>
<script>
var app = angular.module('myApp', [])
app.controller('myCtrl', function($scope) 则好{
$scope.aNum = ""
$scope.bNum = ""
$scope.cNum = ""
$scope.result = $scope.aNum + $scope.bNum+$scope.cNum
$scope.getResult = function(){
var newResult = Number($scope.aNum)+Number($scope.bNum)-Number($scope.cNum)
$("#result").text(newResult)
}
})
</script>
</body>
</html>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)