Javascript 为什么我的 IE9 不支持画布?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6172637/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 20:33:26  来源:igfitidea点击:

Why does my IE9 does not support canvas?

javascriptcanvasinternet-explorer-9

提问by Pacerier

DiveIntoHTML5 Canvasclaims that IE9 supports canvas.

DiveIntoHTML5 Canvas声称 IE9 支持画布。

However, when I tried doing canvas on IE9, it doesn't work:

但是,当我尝试在 IE9 上做画布时,它不起作用:

Object doesn't support property or method 'getContext'

对象不支持属性或方法“getContext”

I'm using IE9.0.8112.16421:

我正在使用 IE9.0.8112.16421:

enter image description here

在此处输入图片说明

This is the code:

这是代码:

<html>
<head>

</head>
<body style="background:black;margin:0;padding:0;">
<canvas id="canvas" style="background:white;width:100%;height:100%;">noob</canvas>
<script>
var img=new Image();
img.onload=function(){
  var c=document.getElementById('canvas');
  var ctx = c.getContext('2d');
  var left,top;
  left=top=0;
  ctx.drawImage(img,left,top,20,20);
  var f=function(){
    left+=1;
    top+=1;
    c.width=c.width;
    ctx.drawImage(img,left,top,20,20);
  };
  setInterval(f,20);
};
img.src="http://a.wearehugh.com/dih5/aoc-h.png";
</script>
</body>
</html>

回答by Jeff

Two things:

两件事情:

The <canvas>tag should having a corresponding closing tag </canvas>. While some browsers will let you get by with just an opening tag, others (such as Firefox, and perhaps IE) won't.

所述<canvas>标签应具有相应的结束标记</canvas>。虽然有些浏览器只需要一个开始标签就可以让您通过,而其他浏览器(例如 Firefox,也许还有 IE)则不会。

In addition, IE9 requires you to declare an HTML5 doctype to use the canvas tag. You can do this by placing <!DOCTYPE html>at the top of your code.

此外,IE9 要求您声明 HTML5 文档类型以使用 canvas 标签。您可以通过放置<!DOCTYPE html>在代码的顶部来做到这一点。

回答by Mike Strother

If IE9 is running in compatibility mode, the canvas tag will not be recognized, even if you are using the HTML5 DOCTYPE tag. At least that has been my experience.

如果 IE9 在兼容模式下运行,则无法识别画布标签,即使您使用的是 HTML5 DOCTYPE 标签。至少这是我的经验。

Check to see if IE9 is running in compatibility mode, which can be the case if the site is on an intranet.

检查 IE9 是否在兼容模式下运行,如果站点位于 Intranet 上,则可能是这种情况。

回答by Tadiotto

<!DOCTYPE html>must be the first thing on your page. I had a script before the tag and it kept giving me the same error. This only happens in IE9, Chrome and Firefox work even with a script before the doctype tag.

<!DOCTYPE html>必须是您页面上的第一件事。我在标签之前有一个脚本,它一直给我同样的错误。这仅发生在 IE9、Chrome 和 Firefox 中,即使在 doctype 标记之前使用脚本。

回答by Ray

Running IE 9.0.8112.16421. Canvas element won't work initially but if I hit F12 to bring up devtools, then refresh, it starts working. Normal refresh w/o devtools window open won't work. May be a JS init problem since the canvas was working before I moved the canvas drawing code into its own .js file. Works fine in Chrome/Firefox/Safari.

运行 IE 9.0.8112.16421。画布元素最初不起作用,但如果我按 F12 键调出 devtools,然后刷新,它就会开始工作。没有打开 devtools 窗口的正常刷新将不起作用。可能是 JS 初始化问题,因为在我将画布绘图代码移动到它自己的 .js 文件之前画布正在工作。在 Chrome/Firefox/Safari 中工作正常。

回答by Karen

when use IE10 in compatibility view please check IE compatibility version. Enter F12 (open developer Tools) and check appropriate IE (for example IE9) version you need test and browser will job right under this version.

在兼容性视图中使用 IE10 时,请检查 IE 兼容版本。输入 F12(打开开发人员工具)并检查您需要测试的相应 IE(例如 IE9)版本,浏览器将在此版本下工作。

回答by TzeMan

guys!

伙计们!

Check out Explorer Canvas on Google Code: https://code.google.com/p/explorercanvas/

在 Google 代码上查看 Explorer Canvas:https: //code.google.com/p/explorercanvas/

Hope this helps!

希望这可以帮助!

Tze