javascript - 属性 onclick ="function()"未按预期运行?

标签 javascript html button onclick

我尝试解决这个问题,但似乎找不到解决方案。经过几个小时的摆弄,我发现问题出在按钮标签或 onclick 属性中(如果我错了,请纠正我)。

我的目标是通过按钮让白点移动

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script type="text/javascript">
        window.onload = function()
        {
            var canvas =        document.getElementById("canvas");
            var ctx = canvas.getContext("2d");
            canvas.width = 345;
            canvas.height = 410;
            canvas.style.border = "1px solid white";

            var left = document.getElementById("left");
            left.onclick = "playerLeft()";

            var x = Math.round(canvas.width/2);
            var y = Math.round(canvas.width/2);
            var rectWidth = 5, rectHeight = 5;
            var fps = 30, frames = 1000/fps;
            var colour = "white";
            var trailColour = "blue";

            ctx.fillStyle = colour;
            ctx.fillRect(x,y,rectWidth,rectHeight);

            function playerLeft() { 
                ctx.fillStyle = trailColour;
                ctx.fillRect(x,y,rectWidth,rectHeight);
                x -= 6; 
                ctx.fillStyle = colour;;
                ctx.fillRect(x,y,rectWidth,rectHeight);
            }
            function playerRight() { 
                ctx.fillStyle = trailColour;
                ctx.fillRect(x,y,rectWidth,rectHeight);
                x += 6; 
                ctx.fillStyle = colour;
                ctx.fillRect(x,y,rectWidth,rectHeight);
            }
            function playerUp() { 
                ctx.fillStyle = trailColour;
                ctx.fillRect(x,y,rectWidth,rectHeight);
                y -= 6; 
                ctx.fillStyle = colour;
                ctx.fillRect(x,y,rectWidth,rectHeight);
            }
            function playerDown() { 
                ctx.fillStyle = trailColour;
                ctx.fillRect(x,y,rectWidth,rectHeight);
                y += 6;
                ctx.fillStyle = colour;
                ctx.fillRect(x,y,rectWidth,rectHeight);
            }
        }
    </script>
</head>
<body style="background-color: black">
    <canvas id="canvas"></canvas>

    <div style="text-align: center; padding: 5px">
        <button id="left">Left</button>
        <button id="up">Up</button>
        <button id="down">Down</button>
        <button id="right">Right</button>
    </div>
</body>
</html>

最佳答案

不使用 onxyz="..." 样式事件处理程序的众多原因之一是,从它们调用的任何函数都必须是全局。您的函数不是,它们是 window.onload 回调的本地函数。

相反,直接分配函数引用,这有点老式:

left.onclick = playerLeft;

或者使用允许多个处理程序的更现代的技术:

left.addEventListener("click", playerLeft);

关于javascript - 属性 onclick ="function()"未按预期运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42514383/

相关文章:

c# - 如何根据 asp.net 中的分辨率以编程方式创建填充客户端浏览器的按钮列表

javascript - AJAX 和辅助功能 : focus or tab index on active element

html - ASP.NET MVC : render view to generate PDF: use iTextSharp or better solution?

javascript - 在 HTML 中单击按钮时调用 JavaScript 函数

javascript - 如何合并两个数组以在 JavaScript 中创建一个数组?

javascript - WebDriver NodeJS 使用功能实例化 IE 驱动程序 : introduceFlakinessByIgnoringProtectedModeSettings(ignoreSettings)

javascript - Uncaught ReferenceError : sendEmail is not defined

html - 重复线性渐变在 Edge 浏览器和 Internet Explorer 浏览器中不起作用

button - ActionEvent 获取按钮 JavaFX 的来源

android - 自定义按钮栏中按钮之间的分隔符