简体   繁体   English

HTML5 Javascript SWITCH不在头部或身体上运行

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

I have a simple code written in JS and embedded in the html5 document. 我有一个用JS编写并嵌入html5文档的简单代码。 Cannot get this thing to run no matter where I place the script part (head or body). 无论我将脚本部件(头部或身体)放在何处,都无法运行此程序。 I have enabled all content in my browsers but none of them runs the script. 我已经在浏览器中启用了所有内容,但是没有一个运行脚本。

Any ideas where the issue could be? 有什么想法可以解决这个问题吗?

<!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>

Your semi-colon looking characters ( ; ) are actually showing up as the unicode : 分号字符( ; )实际上显示为unicode

U+037E : GREEK QUESTION MARK U + 037E:希腊问号

Rather than the correct semi-colons ( ; ): 而不是正确的分号( ; ):

U+003B : SEMICOLON U + 003B:SEMICOLON

With the "wrong semi-colon", you see this error in the browser developer console (usually press F12 to open): 使用“错误的分号”,您会在浏览器开发人员控制台中看到此错误(通常按F12键打开):

Uncaught SyntaxError: Unexpected token ILLEGAL Uncaught SyntaxError:意外的令牌非法

JavaScript supports unicode directly in source files, so you have to be very careful about which characters are used in the source. JavaScript直接在源文件中支持unicode,因此您必须非常小心源中使用了哪些字符。

Your problem here is that your ";" 您的问题是您的“;” semicolons have the wrong encoding. 分号的编码错误。

This is sample example as like your code. 这是示例示例,就像您的代码一样。 It working fine. 工作正常。

  <!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