2016-01-27 63 views
0

我有一個用JS編寫的簡單代碼,嵌入在html5文檔中。無論我放置腳本部分(頭部還是身體),都無法讓這個東西運行。我啓用了瀏覽器中的所有內容,但沒有一個運行腳本。HTML5 Javascript SWITCH不在頭部或身體內運行

任何想法的問題可能是?

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

檢查控制檯錯誤..也許會給你一個線索。\ – putvande

+0

你用什麼編輯器編程?看起來你的代碼中有不需要的(不可見的)字符。 – putvande

+0

我複製你的代碼,它有有趣的分號..也許一些奇怪的特殊字符導致此..否則這似乎爲我工作..經典複製粘貼問題.. – VDP

回答

4

您分號尋找字符(;)實際上showing up as the unicode

U+037E : GREEK QUESTION MARK

而不是正確分號(;):

U+003B : SEMICOLON

隨着 「錯誤分號」 ,您會在瀏覽器開發人員控制檯中看到此錯誤(通常按F12打開):

Uncaught SyntaxError: Unexpected token ILLEGAL

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

+0

耶穌,我完全不知所措。 –

+0

謝謝 - 我使用了MS表達式web 4,它沒有顯示任何東西......我將使用記事本++ – whada

+0

@whada這不一定能解決問題 - 它不是你使用的軟件,而是你正在鍵入的字符進去。 IE這可能與您的鍵盤輸入設置和鍵盤上的字符更相關。 –

2

你的問題在於你的「;」分號有錯誤的編碼。

-1

這是與您的代碼類似的示例示例。它工作正常。

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