将javascript放在html5的外部文件中 [英] place javascript in html5 in external file

查看:204
本文介绍了将javascript放在html5的外部文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在html5中,我如何将下面的类(构造函数和方法)放在另一个文件中并在html文件中引用它。

In html5 how do i place the class below (constructor and method)in another file and reference it in the html file.

下面我有1个文件中的所有内容我不想那样。

Below I have everything in 1 file and I dont want that.

  <canvas id="myCanvas" width="600" height="400"></canvas>
<script>
  var canvas = document.getElementById('myCanvas');
  var ctx = canvas.getContext('2d');
  ctx.font="14px Arial"; 



  //how to move to other file
  function ClassPerson(gender,name1) {
      var aa;
      this.gender=gender;
      this.name1=name1;
      aa=6;

  };

   //how to move to other file
  ClassPerson.prototype.m_sayGender = function()
    {
    ctx.fillText("this gender= " + this.gender + " gender=" + this.name1,10,40);
    };

    //stay in this file
  var person1 = new ClassPerson('male','dave');
  var person2 = new ClassPerson('female','bet');
  ctx.fillText("this gender= " + person1.gender,10,20);
  person1.m_sayGender();
  myObject._three();

</script>


推荐答案

您只需使用代码$创建一个外部JS文件b $ b并包含如下:

You simply create an external JS file with your code and include it like this:

< script src =myFile.js> < / script>

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

确保将JS文件放在您拥有HTML文件的目录中

Be sure to place the JS file in the directory where you have your HTML file

评论后:我猜你必须用javacript动态创建一个图像,设置它的 src porpertie和它的显示。

After comment: I guess you have to dynamically create an image with javacript set it's src porpertie and the diplay it.

这篇关于将javascript放在html5的外部文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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