首页 > 解决方案 > 我的代码声称有一个意外的令牌

问题描述

    <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”在屏幕上弹出,就像被输入一样。我用以下建议修复了它

标签: javascriptsyntax-error

解决方案


在 for 循环中更改,。 另外,删除之前 的.;
for (i=0; i<txt.length;i++){
}</script>


推荐阅读