2015-07-03 46 views
0

所以,我試圖爲字母替換密碼做一個小算法,但當程序試圖打印結果時遇到了問題。對鉻reutrns JS的控制檯「遺漏的類型錯誤:無法讀取空的特性‘的innerHTML’」上線55,77和99Javascript函數返回未捕獲TypeError:無法讀取屬性'innerHTML'null

這裏是代碼:

<!DOCTYPE html> 
<html> 

<head> 
    <title>Alphabetical Subtitution Cipher</title> 
</head> 

<body> 

    <h1>Alphabetical Subtitution Cipher</h1> 

    <input type="text" id ="txt"></input> 
    <button type="button" onclick="encrypt()">Encrypt!</button> 
    <button type="button" onclick="">Decrypt!</button> 
    <button type="button" onclick="res.innerHTML = ''; ">Clear!</button> 

    <div id="result"></div> 


    <script> 

     var letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']; 
     var numbers = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0']; 
     var symbols = ['~', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '-', '=', '+', '[', '{', ']', '}', '/', ';', ':', '"', '|', '.', ',', '<', '>', '?']; 

     var txt = document.getElementById('txt'); 
     var res = document.getElementById('res'); 

     function encrypt() { 

      var spl = txt.value.toLowerCase().split(""); 

      for(var i = 0; i < spl.length; i++) { //goes through the spl array 

       if (spl[i] == ' ') { //if the string is a space 
        res.innerHTML += ' '; //prints space 
       } 

       else { 

        for(var j = 0; j < letters.length; j++) { //goes through the letters array 

         if(spl[i] == letters[j]) { //if the spl array string equals to the letter array string 

          var ecip = (j * i) + spl.length; 

          if (ecip > letters.length) { //if ecip is out of the array 

           ecip = ecip - letters.length; 
           res.innerHTML += letters[ecip]; 

          } 
          else { 

           res.innerHTML += letters[ecip]; 

          } 

         } 

        } 

        for(var k = 0; k < numbers.length; k++) { //goes through the numbers array 

         if (spl[i] == numbers[k]) { //if the spl array string equals to the numbers array currently checked string 

          var ecip = (k * i) + spl.length; 

          if (ecip > numbers.length) { //if ecip is out of the array 

           ecip = ecip - numbers.length; 
           res.innerHTML += numbers[ecip]; 

          } 
          else { 

           res.innerHTML += numbers[ecip]; 

          } 

         } 

        } 

        for(var l = 0; l < symbols.length; l++) { //runs through the symbols array 

         if(spl[i] == symbols[l]) { //if the spl array string equals to the symbols array string 

          var ecip = (l * i) + spl.length; 

          if (ecip > symbols.length) { //if ecip is out of the array 

           ecip = ecip - symbols.length; 
           res.innerHTML += symbols[ecip]; 

          } 
          else { 

           res.innerHTML += symbols[ecip]; 

          } 

         } 

        } 
       } 


      } 




     } 


    </script> 


</body> 

</html> 

我會強烈,如果你欣賞它可以幫助我並解釋我犯了什麼錯誤!謝謝!

回答

-1

您的div的ID是結果,而不是水庫

0

也許你需要把一個反斜槓渲染符號,因爲HTML和JavaScript瞭解一些像HTML代碼。 Html symbols

相關問題