【发布时间】:2010-08-20 21:17:42
【问题描述】:
我遇到了一个奇怪的问题:代码无法访问 canvas 元素。考虑这段代码:
this.canvas = document.getElementById('canvas');
this.context2D = this.canvas.getContext('2d');
Firefox 会产生一个错误,比如this.canvas is null。但如果我这样写:
this.canvas = $('#canvas');
this.context2D = this.canvas.getContext('2d');
Firefox 会告诉getContext is not a method。我查看了this.canvas 并看到了一个不熟悉的对象(不知道它来自哪里,但它绝对不是画布)。
这不是 Firefox 独有的,Chrome 也会产生相同的结果。我快疯了。
编辑:整个 html 都在这里
<!DOCTYPE HTML>
<html lang="en">
<head>
<title>Background test</title>
<script type="text/javascript" src="Scripts/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="Scripts/soundmanager2.js"></script>
<script type="text/javascript" src="Scripts/base.js"></script>
<script type="text/javascript" src="Scripts/Resources.js"></script>
<script type="text/javascript" src="Scripts/Actor.js"></script>
<script type="text/javascript" src="Scripts/Commons.js"></script>
<script type="text/javascript" src="Scripts/Graphics.js"></script>
<script type="text/javascript" src="Scripts/Utils.js"></script>
<script type="text/javascript">
window.onload = function(){
new Commons().startupCommons();
new Graphics().startupGraphics();
}
</script>
<style type="text/css">
body { font-family: Arial,Helvetica,sans-serif;}
canvas {border-style:solid; border-width:5px; border-color:green;}
</style>
</head>
<body>
<canvas id="visualcanvas" width="800" height="600">
<p>
Your browser does not support the canvas element.
</p>
</canvas>
</body>
</html>
我刚刚添加了 window.onload,但问题仍然存在。前面代码中的this指的是Graphics对象,在window.onload触发时调用。
【问题讨论】:
-
不知道在哪里可以上传代码?
-
您可以将其粘贴到问题中。
标签: javascript html canvas