x
1
<p>点击按钮获取数组中大于 18 的所有元素。</p>
2
<button onclick="myFunction()">点我</button>
3
<p id="demo"></p>
4
<script>
5
var ages = [32, 33, 16, 40];
6
7
function checkAdult(age) {
8
return age >= 18;
9
}
10
11
function myFunction() {
12
document.getElementById("demo").innerHTML = ages.filter(checkAdult);
13
}
14
</script>