源代碼
點擊運行
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>教程</title> </head> <body> <p>點擊按鈕檢測年齡。</p> 年齡:<input id="age" value="18" /> <p>是否達到投票年齡?</p> <button onclick="myFunction()">點擊按鈕</button> <p id="demo"></p> <script> function myFunction() { var age,voteable; age=document.getElementById("age").value; voteable=(age<18)?"年齡太小":"年齡已達到"; document.getElementById("demo").innerHTML=voteable; } </script> </body> </html>
運行結果