实例代码“Ctrl+/”提示“F11/ESC”全屏 返回 格式化 恢复 运行
x
 
1
<!DOCTYPE html>
2
<html>
3
<head>
4
<meta charset="utf-8">
5
<title>手册网教程(shouce.ren)</title>
6
</head>
7
<body>
8
9
<p>点击按钮计算数组所有元素相加的总和。</p>
10
<button onclick="numbers.forEach(myFunction)">点我</button>
11
12
<p>数组元素总和:<span id="demo"></span></p>
13
14
<script>
15
var sum = 0;
16
var numbers = [65, 44, 12, 4];
17
18
function myFunction(item) {
19
    sum += item;
20
    demo.innerHTML = sum;
21
}
22
</script>
23
24
</body>
25
</html>