javascript - 我的代码声称有一个意外的令牌
问题描述
<html>
<head>
<title>lorem ipsum</title>
<style type="text/css">
body{
background-color:#222222;
margin:0px;
padding:0px;
}
#typed{
color:red;
font-size:150%;
float:left;
}
.cursor{
height:24px;
width:2px;
background:lawngreen;
float:left;
opacity:0;
animation:blink 0.75s linear infinite alternate;
}
@keyframes blink{
50% {
opacity:0;
}
100% {
opacity:1;
}
}
</style>
</head>
<body>
<div id="typed"></div>
<div class="cursor"></div>
</body>
<script type="text/javascript">
var i;
var txt='lorem ipsum';
var speed=50;
for (i=0; i<txt.length;i++){
setInterval(addLetter("typed"),speed);
}
function addLetter(word){
document.getElementById(word).innerHTML += txt.charAt(i);
i++;
}
</script>
</html>
我不明白为什么它认为它是错误的,但也许我只是错过了一些东西。Chrome 告诉我该错误是 for 循环中未捕获的语法错误,它说错误是“)”。我整天都在试图弄清楚。
编辑:我想要做的是让变量中的文本“txt”在屏幕上弹出,就像被输入一样。我用以下建议修复了它
解决方案
在 for 循环中更改,
。
另外,删除之前 的.;
for (i=0; i<txt.length;i++){
}
</script>
推荐阅读
- .net - 使用 IIS 在本地运行多个 .NET Core 2.2 API
- python - 尝试将循环插入 __init__
- jakarta-ee - 无法在 Web Logic derby 数据源上执行语句
- flow - 将 OneNote 身份验证令牌传递给自定义操作
- node.js - 在 Hyperledger Fabric NodeJS 智能合约中,所有参数都应该作为字符串传递吗?
- python - 计算一个字符串中有多少个字母
- macos - 无法在 Mac (Mojave) 上修改文件夹的权限
- javascript - 我需要默认打开可折叠的 div
- javascript - angular-stl-model-viewer 在 Angular 项目中不起作用
- swift - Dart 的 Optional 运算符是枚举,类似于 Swift 的吗?