如果.slice()以空格结尾

时间:2018-09-06 00:42:28

标签: javascript html slice

为什么空格不计入.slice()中。我相信.slice()的最后一个字符是空格时会发生什么,但我不确定。它应将abcd efgh输出到屏幕上,而应输出abcdefgh。有人可以解释这里发生了什么吗?

编辑:我不清楚,但是字符串是多行的,因此textContent似乎不起作用。

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>GoP2P</title>
        <style>
        body {
            width: 99%;
            height: 100%;
            background-color: black;
            overflow-wrap: break-word;
            overflow: hidden;
        }
        .terminal {
            color: #21ff28;
            width: 100%;
            height: 90vh;

            font-family: monospace;
            font-size: 1.3em;
            overflow-y: scroll;
            padding-right: 1em;

            animation-duration: 0.5s;
            animation-name: colorthing;
            animation-iteration-count: infinite;
            animation-direction: alternate;
        }
        </style>
    </head>
    <body>"
        <div class="terminal" id="terminal">
        </div>
        <script>
            var code =`
            abcd efgh
            abcd efgh`;
        </script>
        <script>
            var i = 0;
            document.addEventListener("keydown", function () {
                terminal.innerText += code.slice(i, i+5);
                i += 5;
                if (i > code.length) {
                    i=0;
                }
            });

            setInterval(function (){
                var term = document.getElementById("terminal");
                term.scrollTop = term.scrollHeight;
            }, 100);
        </script>
    </body>
</html>

0 个答案:

没有答案