我是HTML5新手我想知道为什么下面的代码不能在屏幕上显示一个矩形?
<!DOCTYPE HTML>
<html>
<head>
<title>WebServer Test</title>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
var userviewcanvas = $("userViewerCanvas");
var userviewcontext = userviewcanvas.getContext("2d");
userviewcontext.fillRect(40, 40, 100, 100);
});
</script>
</head>
<body>
<canvas id="userViewerCanvas" width="200" height="300">this is canvas</canvas>
<div><span id="message"> </span> </div>
<div><span id="stream"></span></div>
</body>
</html>
最佳答案
检查这个
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>WebServer Test</title>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
var userviewcanvas = $("#userViewerCanvas").get(0),
userviewcontext = userviewcanvas.getContext("2d");
userviewcontext.fillRect(40, 40, 100, 100);
});
</script>
</head>
<body>
<canvas id="userViewerCanvas" width="200" height="300">this is canvas</canvas>
<div><span id="message"> </span> </div>
<div><span id="stream"></span></div>
</body>
</html>