我想要有打字机效果,以便我以 {} 开头,并且每 120 毫秒键入一次该单词的每个字母。我几乎用以下代码完成了这一点:
Javascript
<script type='text/javascript'>
var index = 0;
var text = 'hello'
// Here you can put in the text you want to make it type.
function type()
{
document.getElementById('screen').innerHTML += text.charAt(index);
index += 1;
var t = setTimeout('type()',120);
}
</script>
HTML
<html>
<body onload="type()">
...
<div id='screen'>{</div>
...
</body>
</html>
我的问题是,在输入单词时,没有右大括号。因此,在第一个 120 毫秒之后,它看起来像这样:{h
但我希望它看起来像这样{h}
我该如何解决这个问题?
最佳答案
在这里查看 http://jsfiddle.net/p56awrea/2
<script type='text/javascript'>
var index = 0;
var text = 'hello'
var current = ''
// Here you can put in the text you want to make it type.
function type()
{
document.getElementById('screen').innerHTML = '{'+current+'}';
current+=text.charAt(index);
index += 1;
if(index==text.length+1){
current = '';
index=0;
setTimeout('type()',2000);
} else {
setTimeout('type()',120);
}
}
</script>
<body onload="type()">
...
<div id='screen'>{}</div>
编辑:在循环行为之前添加 2 秒延迟
关于javascript - 在两个括号内插入文本 - Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29024820/