Javascript 为什么我不能在HTML5画布中绘制矩形?

Javascript 为什么我不能在HTML5画布中绘制矩形?,javascript,canvas,html5-canvas,Javascript,Canvas,Html5 Canvas,我试图在我的HTML5画布上画一个红色矩形。这是我的HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset=utf-8> <title>My Canvas Experiment</title> <link rel="stylesheet" type="text/css" href="main.css" /> <

我试图在我的HTML5画布上画一个红色矩形。这是我的HTML

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset=utf-8>
    <title>My Canvas Experiment</title>
    <link rel="stylesheet" type="text/css" href="main.css" />
    <script src="plotting.js"></script>
    <!--[if IE]>
      <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js">
      </script>
    <![endif]-->
  </head>
  <body>
    <canvas id="plot"></canvas>
  </body>
</html>
下面是main.css:

body { margin:100px; }

article, aside, figure, footer, header, hgroup, menu, nav, section { 
    display:block;
}

#plot {
    width: 500px;
    height: 400px;
}
为什么页面是空白的?Chrome Web Developer控制台未出现错误。

替换:

ctx.fillStylef00

与:

ctx.fillStyle=f00

替换:

ctx.fillStylef00

与:

ctx.fillStyle=f00

使用window.onload而不是document.onload,保留@stewe的建议

使用window.onload代替document.onload,保留@stewe的建议


检查dom时,您看到了什么?你可能也想试试onready…我在Chrome中运行了它,在控制台中看到了一个错误。未捕获的TypeError:对象的属性“fillStyle”不是函数。谢谢,但是使用ctx.fillStyle=f00;仍然不会使矩形出现。使用onready也是如此。如何检查DOM?检查DOM时您看到了什么?你可能也想试试onready…我在Chrome中运行了它,在控制台中看到了一个错误。未捕获的TypeError:对象的属性“fillStyle”不是函数。谢谢,但是使用ctx.fillStyle=f00;仍然不会使矩形出现。使用onready也是如此。我怎么检查DOM?嘿,就是这样!非常感谢。矩形显示为window.onload=function{…但是,我不明白为什么在文档之后窗口就准备好了。实际上,这个页面为我清除了它。当页面准备好显示时,windows.onload会触发,而DOM准备好时,document.onload会触发。嘿,就是这样!谢谢。矩形显示为window.onload=function{…但是,我不明白为什么在文档之后窗口就准备好了。实际上,这个页面为我清除了它。当页面准备好进行演示时,windows.onload会触发,而当DOM准备好时,document.onload会触发。
body { margin:100px; }

article, aside, figure, footer, header, hgroup, menu, nav, section { 
    display:block;
}

#plot {
    width: 500px;
    height: 400px;
}