繁体   English   中英

Javascript外部文件未正确链接

[英]Javascript external file not linking correctly

这是我写过的第一篇JavaScript文章,除了一些警报,也是我的第一篇文章。 因此,我创建了一个HTML页面和一个外部javascript文件。 这两个文件都在同一目录中,我已经检查了每个文件的拼写。

这是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文件:

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代码正常工作?

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM