源代码
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>享学课堂</title> </head> <body> <p>输入你的年龄,点击按钮:</p> <input id="age" value="18" /> <button onclick="myFunction()">试试</button> <p id="demo"></p> <script> function myFunction() { var age, voteable; age = Number(document.getElementById("age").value); if (isNaN(age)) { voteable = "Error in input"; } else { voteable = (age < 18) ? "Too young" : "Old enough"; } document.getElementById("demo").innerHTML = voteable; } </script> </body> </html>
运行结果