getContext(2d)来自动态添加的canvas和jquery

这里有点新手,可能超出了我的深度但是我循环了一些导入的xml并附加一个带有div的容器,然后用canvas附加它,然后我试图绘制到那个canvas。 我得到的只是’getContext()不是一个函数’任何指导感激不尽

var newCanvas = $('', {'class':'cnvsClass'}, {'id': 'theCanvas'}) .width(215) .height(217); $("#innerWrapper") .append($('
', {'class': 'wrapper'}) .append($(newCanvas))); // Have tried $('', $('.cnvsClass'), $("#theCanvas") // I've added [0] after the selector but all I get is // TypeError: $(...).getContext is not a function var ctx = $("#theCanvas").getContext("2d"); var image = new Image(); image.src = "AtlasSheet.png"; $(image).load(function() { ctx.drawImage(image, 830,1165, 215, 217, 0, 0, 215, 217); });

您需要本机DOM对象才能执行此操作。

试试这个;

 var ctx = $("#theCanvas").get(0).getContext("2d");