大家好,今天作为一个技术小编,叫大家如何使用js实现文字一个字一个字的显示,就跟实时文字敲打一样。
<div id="demo"></div>
<script>
const text = "你好,很高兴简单你";
let index = 0;
function showText() {
if (index < text.length) {
document.getElementById("demo").innerHTML += text.charAt(index);
index++;
setTimeout(showText, 30); // 控制文字显示速度,单位为毫秒
}
}
showText();
</script>