Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/403.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 处理onclick事件;未捕获类型错误_Javascript_Onclick_Html5 Canvas - Fatal编程技术网

Javascript 处理onclick事件;未捕获类型错误

Javascript 处理onclick事件;未捕获类型错误,javascript,onclick,html5-canvas,Javascript,Onclick,Html5 Canvas,我这里有个问题,我似乎也找不到解决办法。我试图处理一个onclick事件,它只是在“瓷砖”上切换颜色。以下是我必须执行的脚本: <script> var GameTile = { createTile : function() { var newElement = document.createElement('div'); newElement.id = "tileContainer";

我这里有个问题,我似乎也找不到解决办法。我试图处理一个onclick事件,它只是在“瓷砖”上切换颜色。以下是我必须执行的脚本:

<script>
    var GameTile = {
        createTile : function() {
            var newElement = document.createElement('div');
            newElement.id = "tileContainer";
            newElement.style.width = "100px";
            newElement.style.height = "100px";
            newElement.setAttribute("onclick", "GameTile.convertColor()");
            document.body.appendChild(newElement)

            var canvas = document.createElement('canvas');
            canvas.setAttribute("id", "tileCanvas");
            canvas.setAttribute("width", "100");
            canvas.setAttribute("height", "100");
            var ctx = canvas.getContext("2d");

            ctx.fillStyle = "#000000";
            ctx.fillRect (0, 0, 100, 100);

            ctx.fillStyle = "#969696";
            ctx.fillRect (7, 7, 86, 86);
            document.getElementById("tileContainer").appendChild(canvas);

        },

        convertColor : function() { 

            var canvas = document.getElementById('tileCanvas');
            var ctx = canvas.getContex("2d");

            ctx.fillStyle = "#969696";
            ctx.fillRect (0, 0, 100, 100);

            ctx.fillStyle = "#000000";
            ctx.fillRect (7, 7, 86, 86);

            document.getElementById("tileContainer").appendChild(canvas);
        }

    };
</script>

变量配子片={
createTile:function(){
var newElement=document.createElement('div');
newElement.id=“tileContainer”;
newElement.style.width=“100px”;
newElement.style.height=“100px”;
setAttribute(“onclick”,“GameTile.convertColor()”;
document.body.appendChild(新元素)
var canvas=document.createElement('canvas');
setAttribute(“id”、“tileCanvas”);
canvas.setAttribute(“宽度”、“100”);
canvas.setAttribute(“高度”、“100”);
var ctx=canvas.getContext(“2d”);
ctx.fillStyle=“#000000”;
ctx.fillRect(0,0,100,100);
ctx.fillStyle=“#9696”;
ctx.fillRect(7,7,86,86);
document.getElementById(“tileContainer”).appendChild(画布);
},
convertColor:函数(){
var canvas=document.getElementById('tileCanvas');
var ctx=canvas.getContex(“2d”);
ctx.fillStyle=“#9696”;
ctx.fillRect(0,0,100,100);
ctx.fillStyle=“#000000”;
ctx.fillRect(7,7,86,86);
document.getElementById(“tileContainer”).appendChild(画布);
}
};

就我所知,convertColor()函数实际上是定义的,我调用它是否有误?或者我的实际语法不正确?

您在convertColor函数中有一个输入错误

var ctx = canvas.getContex("2d");
应该是

var ctx = canvas.getContext("2d");
请注意缺少的t。

您应该交换它

newElement.setAttribute("onclick", "GameTile.convertColor()");
对此

newElement.addEventListener("click", "GameTile.convertColor()");

哪里说的是未捕获类型错误?我知道!非常感谢你!我花了大约3个小时查看这段代码,但我甚至没有看到。非常感谢你听到这个消息。现在一切都好了=D