2013-08-28 95 views
0

正如標題所說,這 上的jsfiddle運行我的代碼: http://jsfiddle.net/paopaomj/zgGpN/我有我的代碼在jsfiddle上運行,但爲什麼不能在本地計算機上運行?

,我複製了所有代碼我的本地計算機上(帶支架編輯),但無法運行,這是我錯過? (這裏忽略了CSS樣式)

<html> 
    <head> 
     <script type="text/javascript" src="http://code.jquery.com/jquery-2.0.2.min.js"></script> 
     <!--<script type="text/javascript" src="tools.js"></script>--> 
     <link rel="stylesheet" type="text/css" href="style.css"> 
    </head> 
    <body> 
     <script> 
      $document.ready(function(){ 
       $("#command").keyup(function (e) { 
        if (e.keyCode == 13) { 
         submit(); 
        } 
       }); 

       var submit = function() { 
        var commandEl = document.getElementById("command"); 
        var command = commandEl.value; 
        var outputel = document.getElementById("output"); 
        var new_row = document.createElement("div"); 
        new_row.innerHTML = "[email protected]# > " + command; 
        outputel.appendChild(new_row); 
        commandEl.value=""; 
       }; 
      }) 
     </script> 
     <div id="output"></div> 
     <div id="input"> 
      [email protected]# > 
      <input type="text" id="command" /> 
    </div> 
</body> 
</html> 
+3

檢查您的瀏覽器控制檯查看是否有任何錯誤 –

+0

乍一看,你錯過了一個;在JavaScript的末尾,以及一個。 –

+13

咋一看,我會說它是'$ document.ready',你需要對你的'document'進行jQuery化 - 使用'$(document).ready'來代替。 –

回答

2

兼用,這樣

$(document).ready(function() { 
    // Handler for .ready() called. 
}); 

$(function() { 
    // Handler for .ready() called. 
}); 

,而不是

$document.ready(function(){ 
    // Handler for .ready() called. 
}); 
0

使用命令功能,

  $("#command").on('keyup', function (e) { 
       if (e.keyCode == 13) { 
        submit(); 
       } 
      }); 
0

我糾正你的代碼(錯誤指令「的文件準備好」和格式)

<html> 
    <head> 
     <script type="text/javascript" src="http://code.jquery.com/jquery-2.0.2.min.js"></script> 
     <!--<script type="text/javascript" src="tools.js"></script>--> 
     <link rel="stylesheet" type="text/css" href="style.css"> 
       <script type="text/javascript"> 
      $(document).ready(function(){ 
       $("#command").keyup(function (e) { 
        if (e.keyCode == 13) { 
         submit(); 
        } 
       }); 

       var submit = function() { 
        var commandEl = document.getElementById("command"); 
        var command = commandEl.value; 
        var outputel = document.getElementById("output"); 
        var new_row = document.createElement("div"); 
        new_row.innerHTML = "[email protected]# > " + command; 
        outputel.appendChild(new_row); 
        commandEl.value=""; 
       }; 
      }) 
     </script> 
    </head> 
    <body> 

     <div id="output"></div> 
     <div id="input"> 
      [email protected]# > 
      <input type="text" id="command" /> 
    </div> 
</body> 
</html> 
0

你的語法是不正確的周圍document.ready()$(document).ready(function(){

<!DOCTYPE html> 
<html> 
<head> 
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script> 

<script type="text/javascript"> 
    $(document).ready(function(){ 
     $("#command").keyup(function (e) { 
      if (e.keyCode == 13) { 
       submit(); 
      } 
     }); 
    }); 

var submit = function() { 
     var commandEl = document.getElementById("command"); 
     var command = commandEl.value; 
     var outputel = document.getElementById("output"); 
     var new_row = document.createElement("div"); 
     new_row.innerHTML = "[email protected]# > " + command; 
     outputel.appendChild(new_row); 
     commandEl.value=""; 
    }; 
</script> 

<style type="text/css"> 
body { 
    font-family:'Rosario', sans-serif; 
    background-color: #000000; 
    font-size: 16px; 
    color: #00FF00; 
} 

#output { margin-bottom: 0px; background-color: #000000; } 
#input { margin-top: 0px; background-color: #000000; } 

input { 
    border: 0; 
    background: #000000; 
    color: #00FF00; 
    outline: none; 
    font-family:'Rosario', sans-serif; 
    font-size: 16px; 
    padding: 0px; 
    margin-left: -0.1px; 
    margin: 0px; 
} 
</style> 
</head> 
<body> 
    <div id="output"></div> 
    <div id="input"> 
     [email protected]# > 
     <input type="text" id="command" /> 
    </div> 
</body> 
</html> 
相關問題