jQuery 动态画布创建,$ctx.getContext 不是函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3239680/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery dynamic canvas creation, $ctx.getContext is not a function
提问by Evan Carroll
When I try to execute this in jQuery I get $ctx.getContext is not a function
in firebug.
当我尝试在 jQuery 中执行它时,我进入$ctx.getContext is not a function
了萤火虫。
var $ctx = $( '<canvas />', {width:'100', height:'100'} )
$widget.append($ctx)
$ctx.getContext('2d')
Any idea why I get this error? How do I dynamically create and initialize a canvas element?
知道为什么我会收到这个错误吗?如何动态创建和初始化画布元素?
回答by Scott Evernden
$ctx
is a jQuery object. use $ctx[0].getContext('2d')
to get the context
$ctx
是一个 jQuery 对象。用于$ctx[0].getContext('2d')
获取上下文
回答by Castrohenge
If using excanvas you will need to use the following so it works in IE.
如果使用 excanvas,您将需要使用以下内容,以便它在 IE 中工作。
var canvas = $ctx[0];
if (canvas.getContext == undefined) {
return G_vmlCanvasManager.initElement(canvas).getContext("2d");
}
return canvas.getContext('2d')
回答by Peter
first thing first , you need to check out if the following line is exist and the top of HTML document.
首先,您需要检查以下行是否存在以及 HTML 文档的顶部。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML Strict//EN">
this line as a browser render policy excuted condition.
此行作为浏览器呈现策略执行条件。