实例代码“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
<p>最小年龄: <input type="number" id="ageToCheck" value="18"></p>
11
<button onclick="myFunction()">点我</button>
12
13
<p>索引: <span id="demo"></span></p>
14
15
<p><strong>注意:</strong> IE 11 及更早版本不支持 findIndex() 方法。</p>
16
17
<script>
18
var ages = [4, 12, 16, 20];
19
20
function checkAdult(age) {
21
    return age >= document.getElementById("ageToCheck").value;
22
}
23
24
function myFunction() {
25
    document.getElementById("demo").innerHTML = ages.findIndex(checkAdult);
26
}
27
</script>
28
29
</body>
30
</html>