2016-10-11 155 views
1

這是我第一次寫的JavaScript,這是我從一些警報和第一篇文章中寫出來的。所以我創建了一個Html頁面和一個外部JavaScript文件。這兩個文件都在同一個目錄中,我檢查了每個文件的拼寫。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代碼工作?

+5

你有[檢查控制檯]錯誤(http://stackoverflow.com/documentation/javascript/185/hello-world/714/using-console-log)? –

回答

6

您忘記了函數名稱末尾的()

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

就是這樣!謝謝!!!! – Nuno1895