2016-01-21 24 views
-1

我不斷收到標題中的錯誤。我究竟做錯了什麼?我不想使用jquery。我知道有一個簡單的答案,我實際上只是沒有看到它。Uncaught TypeError:無法讀取屬性'getContext'null幫助請求

<!DOCTYPE html> 
<html> 
<body> 
<canvas id="canvas1" width="500px" height="500px"> 
</canvas> 

<script> 
    var canvas = document.getElementById("#canvas1"); 
    var c = canvas.getContext("2d"); 

    c.beginPath(); 
    c.moveTo(0,0); 
    c.lineTo(100,100); 
    c.stroke(); 
</script> 
</body> 
</html> 
+8

不需要''#。 'getElementById(「#canvas1」);'==>'getElementById(「canvas1」);'' – Tushar

回答

1

你在下面的行

var canvas = document.getElementById("#canvas1"); 

做小的失誤只是instaed指定ID,而無需像#

var canvas = document.getElementById("canvas1"); 
相關問題