【问题标题】:How can I determine why this code is outputting nothing? [closed]我怎样才能确定为什么这段代码没有输出? [关闭]
【发布时间】:2020-12-16 22:17:27
【问题描述】:

我正在学习 JavaScript 动画。我写了这段代码,但它不起作用。我怎样才能找到问题?单击按钮时,我想移动蓝色方块:

    <html>

    <!-- css code -->
    <style>
        body{background-color:black;}
        
        #a{
        width:100;
        height:100;
        position:relative;
        background-color:yellow;
        }
        
        #b{
        width:50px;
        height:50px;
        position:absolute;
        background-color:blue;
        }
        
        #btn{
        background-color:orange;
        border-radius:4px;
        color:white;
        }
    </style>
    <!-- css ends here -->





    <!-- html code-->
    <body>
        <button type="submit" onclick="rn" id="btn">click me</button>
        <div id="a">
            <div id="b"></div>
        </div>    
    </body>
    <!-- html ends here -->





    <!-- script starts here -->
    <script>
        function rn(){
        var box = document.getElementById("b");
        var pos = 0;
        var r = setInterval(move, 5);
        
        function move(){
        if (pos == 150){clearInterval(r)
        }//if ends
        
        else{
            pos +=1;
            box.style.left = pos+"px"
        }//else ends
        }//child function ends
        }//parent function ends
    </script>
    <!-- script ends here -->


    </html>

【问题讨论】:

  • 将onclick="rn" 更改为onclick="rn()"。
  • 我不确定样式或脚本是否会起作用 - 它们既不在 &lt;head&gt; 或 &lt;body&gt; 中。

标签: javascript debugging animation


【解决方案1】:

只需在点击中将 rn 更改为 rn()。它会起作用的。

rn 是函数,你想在点击时执行。

【讨论】:

    猜你喜欢
    • 2012-09-03
    • 2017-08-15
    • 1970-01-01
    • 2013-03-22
    • 1970-01-01
    • 2014-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多