为什么这个 JavaScript 没有在我的
为什么这不起作用?
<html>
<head>
<title>Learning the basics of canvas</title>
<meta charset="utf-8">
<script type="text/javascript" src="http://ajax.googleapis.com?/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
var canvas = $("#myCanvas");
var context = canvas.get(0).getContext("2d");
context.fillRect(40, 40, 100, 100);
});
</script>
</head>
<body>
<canvas id="myCanvas" width="500" height="500">
<!-- Insert fallback content here -->
</canvas>
</body>
</html>
Why won’t this work?
<html>
<head>
<title>Learning the basics of canvas</title>
<meta charset="utf-8">
<script type="text/javascript" src="http://ajax.googleapis.com?/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
var canvas = $("#myCanvas");
var context = canvas.get(0).getContext("2d");
context.fillRect(40, 40, 100, 100);
});
</script>
</head>
<body>
<canvas id="myCanvas" width="500" height="500">
<!-- Insert fallback content here -->
</canvas>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的代码缺少文档类型,并且存在拼写错误。
文档类型:
拼写错误:
应该是
固定代码:
Your code is missing a doctype, and there's a typo.
Doctype:
Typo:
should be
Fixed code:
您需要一个文档类型。在页面最顶部插入
You need a DOCTYPE. Insert
<!DOCTYPE html>
at the very top of your page添加 HTML 5 文档类型标签。许多浏览器默认情况下仍然呈现 XHTML,并且您需要指出这是 HTML 5 格式。
http://www.w3schools.com/html5/tag_doctype.asp
Add the HTML 5 doctype tag. Many browsers are also still rendering XHTML by default and need you'll need to indicate that this is a HTML 5 format.
http://www.w3schools.com/html5/tag_doctype.asp
尝试
canvas.getContext('2d')
,不要介入get(0)
。Try
canvas.getContext('2d')
, without the interveningget(0)
.