Javascript函数返回未捕获的TypeError:无法读取null的属性“innerHTML”

Javascript函数返回未捕获的TypeError:无法读取null的属性“innerHTML”,javascript,html,null,Javascript,Html,Null,所以,我试图为字母替换密码制作一个小算法,但是当程序试图打印结果时,我遇到了一个问题。chrome reutrns Uncaught TypeError上的js控制台:无法读取第55、77和99行的null属性“innerHTML” 代码如下: <!DOCTYPE html> <html> <head> <title>Alphabetical Subtitution Cipher</title> </head>

所以,我试图为字母替换密码制作一个小算法,但是当程序试图打印结果时,我遇到了一个问题。chrome reutrns Uncaught TypeError上的js控制台:无法读取第55、77和99行的null属性“innerHTML”

代码如下:

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

如果您能帮助我并解释我在哪里犯了错误,我将不胜感激!谢谢

div的id是result,而不是res

可能需要加反斜杠来呈现符号,因为html和javascript理解一些类似于html的代码