Chrome中canvas报Cannot call method 'getContext' of null
2012-08-17 17:04
295 查看
今天在看HTML5 的CANVAS,找了一段简单的代码,敲好后,用CHROME打开,竟然报出Cannot call method 'getContext' of null 这个错误,就是说getContext这个方法没有,唉?我就纳闷啦,按理说chrome对html5支持的很好啊,应该不会有这种错误啊,查了些资料也没找到,代码如下
<html><head><script type="text/javascript">var canvas=document.getElementById("screen");var ctx=canvas.getContext("2d");ctx.fillStyle="#f00";ctx.fillRect(30,40,80,100);</script></head><body onload="onReady()"><canvas id="screen" width="600" height="400"></canvas></body></html>后来,我想了很久,觉得是不是因为js先于页面载入,还没有canvas属性啊,于是改改看
<html><head><script type="text/javascript">function onReady(){var canvas=document.getElementById("screen");var ctx=canvas.getContext("2d");ctx.fillStyle="#f00";ctx.fillRect(30,40,80,100);}</script></head><body onload="onReady()"><canvas id="screen" width="600" height="400"></canvas></body></html>看来就是这个问题,要在页面载入后才能获得canvas对象,这个问题在firefox不会出现,看来是做了优化,反正还是注意吧,小问题
相关文章推荐
- canvas 在chrome预览时 Cannot read property 'getContext' of null
- (启动H5本地存储)WebView显示网页出现Uncaught TypeError: Cannot call method 'getItem' of null异常
- TypeError:Cannot call method 'getItem' of null
- apache 的error日志:Pango-CRITICAL **: pango_context_load_font: assertion `pango_font_description_get_family (desc) != NULL' failed
- android webView 图片加载不出来 后台报错Uncaught TypeError: Cannot call method 'getElementsByTagName' of null
- echarts-all.js:1 Uncaught TypeError: Cannot read property 'getAttribute' of null
- SharePoint 2013 Error - TypeError: Unable to get property 'replace' of undefined or null reference
- Extjs4---Cannot read property 'addCls' of null
- Permission denied to access property 'dom' from a non-chrome context
- spring.net错误排查 Error creating context 'spring.root': InputStream is null from Resource
- Extjs4---Cannot read property 'addCls' of null
- JS中“null'document.getElementById为空或不是对象” 的解决办法
- 【cocos2d-js系列问题】Cannot read property 'x' of null错误解决办法
- Extjs4---Cannot read property 'addCls' of null
- JS中“null'document.getElementById为空或不是对象” 的解决办法
- couldn't get cmd pointer (substituting NULL): extracting data from value failed Couldn't materialize
- 【ExtJS】Cannot call method 'removeCls'或'addCls' of null
- Uncaught TypeError: Cannot set property 'innerHTML' of null
- Uncaught TypeError: Cannot set property 'innerHTML' of null
- Canvas使用错误:screen.getContext('2d') is not a function