<!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 οnclick="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>