W3School TIY Editor
W3School 在线教程
改变方向
暗黑模式
运行代码
<!DOCTYPE html> <html> <body> <p>在输入字段中输入 Banana、Orange 或 Apple,然后单击按钮。</p> <p>switch 语句将根据您的输入执行一段代码。</p> <input id="myInput" type="text"> <button onclick="myFunction()">试一试</button> <p id="demo"></p> <script> function myFunction() { var text; var fruits = document.getElementById("myInput").value; switch(fruits) { case "Banana": text = "Banana is good!"; break; case "Orange": text = "I am not a fan of orange."; break; case "Apple": text = "How you like them apples?"; break; default: text = "I have never heard of that fruit..."; } document.getElementById("demo").innerHTML = text; } </script> </body> </html>