2016-10-11 117 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