gpt4 book ai didi

javascript - 如何在脚本中添加新行

转载 作者:行者123 更新时间:2023-11-28 16:18:32 30 4
gpt4 key购买 nike

你好,我不知道我的标题是否有帮助,但这是我的问题添加了它不显示的新行。

	var str = "<p>I want to put text here then another line under this one</p>",
<!--var str = "<p>text here</p>",--> <!--This is what I tried to do to add a new line-->
i = 0,
isTag,
text;

(function type() {
text = str.slice(0, ++i);
if (text === str) return;

document.getElementById('typewriter').innerHTML = text;

var char = text.slice(-1);
if( char === '<' ) isTag = true;
if( char === '>' ) isTag = false;

if (isTag) return type();
setTimeout(type, 80);
}());
#typewriter {
color: lime;
text-align: center;
}
 <div id="typewriter"></div>

最佳答案

var str = "My text\nSome more text";
var stra = str.split("");
var tw = document.getElementById("output");
function type(){
var char = stra.shift();
if (char){
tw.innerHTML += char;
setTimeout(type, 80);
}

}
type();
<pre id="output"></pre>

关于javascript - 如何在脚本中添加新行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37261057/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com