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