为什么这个 JavaScript 没有在我的中绘制一个矩形?标签?

发布于 2024-11-05 07:38:31 字数 945 浏览 1 评论 0原文

为什么这不起作用?

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

等风也等你 2024-11-12 07:38:31

您的代码缺少文档类型,并且存在拼写错误。

文档类型:

<!DOCTYPE html>

拼写错误:

http://ajax.googleapis.com?/ajax/libs/jquery/1/jquery.min.js

应该是

http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js

固定代码:

<!DOCTYPE html>
<html> 
        <head> 
                <title>Learning the basics of canvas</title> 
                <meta charset="utf-8"> 
                <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script> 
                <script>
                        //<![CDATA[
                        $(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>

Your code is missing a doctype, and there's a typo.

Doctype:

<!DOCTYPE html>

Typo:

http://ajax.googleapis.com?/ajax/libs/jquery/1/jquery.min.js

should be

http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js

Fixed code:

<!DOCTYPE html>
<html> 
        <head> 
                <title>Learning the basics of canvas</title> 
                <meta charset="utf-8"> 
                <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script> 
                <script>
                        //<![CDATA[
                        $(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>
请别遗忘我 2024-11-12 07:38:31

您需要一个文档类型。在页面最顶部插入

You need a DOCTYPE. Insert <!DOCTYPE html> at the very top of your page

画骨成沙 2024-11-12 07:38:31

添加 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

少女的英雄梦 2024-11-12 07:38:31

尝试 canvas.getContext('2d'),不要介入 get(0)

Try canvas.getContext('2d'), without the intervening get(0).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文