Javascript 外部文件未正确链接 [英] Javascript external file not linking correctly

查看:25
本文介绍了Javascript 外部文件未正确链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我写的第一个 javascript,除了一些警报和我的第一篇文章.所以我创建了一个 Html 页面和一个外部 javascript 文件.两个文件都在同一个目录中,我已经检查了每个文件的拼写.

This is my first javascript that I have ever wrote aside from a few alerts and also my first post. So I have created an Html page and a external javascript file. Both files are in the same directory and I have checked the spellings of each file.

这是html代码:

<body>

  <p>Press the buttons to change the box!</p>

   <div id="box" style="height:150px; width:150px; background-color:orange; margin:25px"></div>

     <button id="button1" onclick="divGrow">Grow</button>
     <button id="button2" onclick="divColor">Blue</button>
     <button id="button3" onclick="divFade">Fade</button>
     <button id="button4" onclick="divReset">Reset</button>

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

  </body>

这里是javascript文件:

and here is the javascript file:

function divGrow() {

   alert("This grows the box");
   document.getElementById("box").style.width = "300px";
   document.getElementById("box").style.height = "300px";

 }

 function divColor() {

   alert("This changes the Box to Blue");
   document.getElementById("box").style.backgroundColor = "Blue";

  }

  function divFade() {

   alert("This fades the box");
   document.getElementById("box").style.opacity = ".5";

  }

 function divReset() {

  alert("This resets the box")
  document.getElementById("box").style.backgroundColor = "Orange"
  document.getElementById("box").style.width = "150px";
  document.getElementById("box").style.height = "150px";
  document.getElementById("box").style.opacity = "0";

 }

我错过了另一个链接吗?使 JavaScript 代码正常工作?

Am I missing another link? To make the javascript code work?

推荐答案

您忘记了函数名称末尾的 ().

You forgot the () at the end of the function names.

 <button id="button1" onclick="divGrow()">Grow</button>
 <button id="button2" onclick="divColor()">Blue</button>
 <button id="button3" onclick="divFade()">Fade</button>
 <button id="button4" onclick="divReset()">Reset</button>

这篇关于Javascript 外部文件未正确链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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