簡體   English   中英

HTML5 Javascript SWITCH不在頭部或身體上運行

[英]HTML5 Javascript SWITCH not running in head or body

我有一個用JS編寫並嵌入html5文檔的簡單代碼。 無論我將腳本部件(頭部或身體)放在何處,都無法運行此程序。 我已經在瀏覽器中啟用了所有內容,但是沒有一個運行腳本。

有什么想法可以解決這個問題嗎?

<!DOCTYPE html>
<html>
   <title>tryme</title>
   <head>
   </head>
   <body>
      <script>
        var instr = prompt("which instrument do you play?");
        switch(instr) {
            case "violin":
            case "piano":
                alert("Me too!");
                break;
            case "drums":
            case "ukulele":
                alert("sonds good");
                break;
            case "whistle":
                alert("omg!!!");
                break;
            default:    
                alert("whatever...");
        }
      </script>
      <noscript>"your browser doesnt support javascript"</noscript>
   </body>
</html>

分號字符( ; )實際上顯示為unicode

U + 037E:希臘問號

而不是正確的分號( ; ):

U + 003B:SEMICOLON

使用“錯誤的分號”,您會在瀏覽器開發人員控制台中看到此錯誤(通常按F12鍵打開):

Uncaught SyntaxError:意外的令牌非法

JavaScript直接在源文件中支持unicode,因此您必須非常小心源中使用了哪些字符。

您的問題是您的“;” 分號的編碼錯誤。

這是示例示例,就像您的代碼一樣。 工作正常。

  <!DOCTYPE html> <html> <title>tryme</title> <head> </head> <body> <script type="text/javascript"> var instr = prompt("which instrument do you play?"); switch(instr){ case "2": case "1":alert('hello'); break; case "dd": case "tt":alert('hello tt'); break; default:alert("no any"); } </script> <noscript>"your browser doesnt support javascript"</noscript> </body> </html> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM