1. 程式人生 > >canvas中的Uncaught TypeError: Cannot read property 'getContext' of null 錯誤

canvas中的Uncaught TypeError: Cannot read property 'getContext' of null 錯誤

HTML5已經 出生三年有餘,的使用也愈發廣泛。

在使用canvas.getContext(‘2d’)的時候,

出現:

Uncaught TypeError: Cannot read property ‘getContext’ of null 錯誤

最為可能的原因是在html元素載入之前就運行了JavaScript,所以解決方法有二:

1. 在$(document).ready(function(){})寫canvas程式碼

預載入。

2. 將script扔到body的最下面

這裡寫圖片描述