js中forEach的用法
阿新 • • 發佈:2018-10-11
reac document input UNC button ctype tle dex charset
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>forEach的用法</title>
</head>
<body>
<span id="number"></span>
<input type="text" id="mul">
<button onclick="arr1.forEach(myFunction)">點擊計算</button>
<p>計算後的結果:<span id="mulres"></span></p>
<script>
var num = 0;
var arr = [2,3,123,67,89,34];
function myFuc(item){// 數組中每個元素需要調用的函數
num += item;
document.getElementById(‘number‘).innerHTML = num;
}
// console.log(arr.forEach(myFuc));
var arr1 = [3,4,22,1,3,78];
function myFunction(item,index,arr){
arr[index] = item*document.getElementById(‘mul‘).value;
document.getElementById(‘mulres‘).innerHTML = arr1;
}
</script>
</body>
</html>
js中forEach的用法