1

我正在尝试包含一个单独的 .js 文件,这样我就可以引入我创建的一个类,但它不起作用,有人能告诉我为什么吗?

玩家角色.js

function PlayerCharacter() {
this.x = 0; 
this.y = 0;

this.dx = 5;
this.dy = 5;
}


PlayerCharacter.prototype.sayHello = function()
{
 alert ('hello');
};

索引.html

<html>
<head>
<script src="js/playercharacter.js" type="text/javascript" ></script>
</head>
<body>
 <script type="text/javascript" >
var player = new PlayerCharacter();
player.sayHello();
</script>
</body>
</html>
4

3 回答 3

1

您忘记将 JS 代码包装到<script>标签中

<html>
 <head>
  <script src="js/playercharacter.js" type="text/javascript" ></script>
 </head>
 <body>
  <script type="text/javascript" >
   var player = new PlayerCharacter();
   player.sayHello();
  </script>
 </body>
</html>
于 2013-05-14T00:42:37.253 回答
0

您的 JavaScript 代码不在script标签中。

<script>
    var player = new PlayerCharacter();
    player.sayHello();
</script>
于 2013-05-14T00:42:39.793 回答
0

您需要用标签将正文中的<script>代码括起来。

于 2013-05-14T00:43:39.733 回答