外部Javascript代码无法正常工作 [英] External Javascript code not working

查看:87
本文介绍了外部Javascript代码无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在尝试学习javascript和html5,我创建了这个文件。

但是,外部的js文件不会在我的画布上绘制矩形。如果我将它移动到html页面,它工作正常,这是什么问题?请允许任何人指出..





Hi,
I am trying to learn javascript and html5, and I have created this file.
But, the external js file dont draw rectangle on my canvas.. if i move it to html page, it works fine, whats the problem? can anybody pointout please..


<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script type='text/javascript' src = '/js/main.js'>
</script>
<canvas id="myCanvas" width = "1300" height = "800" style="border: 1px solid black">Your browser does not support 

the HTML5 canvas tag.</canvas>
<script>
var c=document.getElementById('myCanvas');
drawRectangle(c);
</script>
</body>
</html>





这里是文件夹中的js文件js ...

Main.js





here is the js file in folder js...
Main.js

function drawRectangle(c)
{ 
var ctx=c.getContext('2d');
ctx.fillStyle='#FF0000';
ctx.fillRect(0,0,80,100);
}

推荐答案

错误是

the error is
引用:

获取http://127.0.0.1:8020/js/main.js 404(未找到)index.html:7

未捕获的ReferenceError:drawRectangle未定义

GET http://127.0.0.1:8020/js/main.js 404 (Not Found) index.html:7
Uncaught ReferenceError: drawRectangle is not defined





将源代码更改为



change your source code to

<script type='text/javascript' src = 'js/main.js'>


一切都很好看。删除src属性之前和之后不必要的空格并尝试:



Everything is looking fine. Remove unnecessary space before and after src attribute and try:

<script type="text/javascript" src="/js/main.js">
</script>


您好Yesudasan,

我尝试了您的代码。它运行正常。



Hi Yesudasan,
I tried your code.It is working properly.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="js/main.js"></script>
    <script>
        var c = document.getElementById('myCanvas');
        drawRectangle(c);
    </script>
</head>
<body>
    <canvas id="myCanvas" width="1300" height="800" style="border: 1px solid black">Your browser does not support
 the HTML5 canvas tag.</canvas>
</body>
</html>


这篇关于外部Javascript代码无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆