简体   繁体   English

如何从 JavaScript 一个字母一个字母地打印多行文本?

[英]How to print multi-line text letter by letter from JavaScript?

I want to print my text letter by letter using JavaScript.我想使用 JavaScript 一个字母一个字母地打印我的文本。 I tried the following answer https://stackoverflow.com/a/7265613/7920589 which is in jQuery by converting it into plain javascript.我尝试了以下答案https://stackoverflow.com/a/7265613/7920589 ,它在 jQuery 中,通过将其转换为纯 javascript。

     let showText = function (target, message, index, interval) {
            if (index < message.length) {
                document.querySelector(target).innerText += message[index++];

                setTimeout(function () { showText(target, message, index, interval); }, interval);
            }
            }
     showText("#msg", "Hello this line is printing letter by letter\n hello this line too is printing letter by letter,", 0, 200);

I tried this code but though it prints the message letter by letter, it completely ignores the spaces in the message.我试过这段代码,但虽然它一个字母一个字母地打印消息,但它完全忽略了消息中的空格。

OUTPUT:输出:

Hellothislineisprintingletterbyletter
hellothislinetooisprintingletterbyletter,

I tried replacing innerText with innerHTML and textContent but then the new line escape character \\n does not work and instead prints a simple space instead on going to a new line.我尝试用innerHTMLtextContent替换innerText ,但是新行转义字符\\n不起作用,而是打印一个简单的空格而不是转到新行。

OUTPUT:输出:

Hello this line is printing letter by letter hello this line too is printing letter by letter,

Please help me with this.请帮我解决一下这个。

You could use a <code> tag in combination with textContent and white-space: pre-wrap :您可以将<code>标签与textContentwhite-space: pre-wrap结合使用:

 let showText = function(target, message, index, interval) { if (index < message.length) { document.querySelector(target).textContent += message[index++]; setTimeout(function() { showText(target, message, index, interval); }, interval); } } showText("#msg", "Hello this line is printing letter by letter\\n hello this line too is printing letter by letter,", 0, 20);
 code { white-space: pre-wrap }
 <code id="msg"></code>

An alternative to what CertainPerformance proposed is to slice the text.对某些CertainPerformance 建议的替代方法是对文本进行切片。

 let showText = function(target, message, index, interval) { if (index < message.length) { document.querySelector(target).textContent = message.slice(0, index); setTimeout(function() { showText(target, message, index + 1, interval); }, interval); } } showText("#msg", "Hello this line is printing letter by letter\\n hello this line too is printing letter by letter,", 0, 20);
 div { white-space: pre-wrap; }
 <div id="msg"></div>

maybe you should try this kind of loop to print letter by letter也许你应该尝试这种循环来逐个打印

<input type="text" id="input1">
<button onclick="his()">click</button>

and the js和 js

function his() {
    let input = document.getElementById("input1").value
    for (let index of input) {
        console.log(index)
    }
}

i just discover this type of loop and its kinda has short syntax.我只是发现这种类型的循环,它的语法有点短。 maybe this could help也许这会有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM