2014-10-16 63 views
-3

我已經在我的HTML文件下面的代碼Javascript和jQuery的不工作時,我把它放到一個單獨的js文件

<head> 
    <link href = "css/bootstrap.min.css" rel="stylesheet"> 
    <link href = "css/aboutmestyle.css" rel="stylesheet"> 
    <script src= "js/aboutme.js" type="text/javascript"></script> 

    <script src = "http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"> 
    </script> 
    <script src= "js/bootstrap.js"> 
    </script> 

    <script type="text/javascript"> 
    $(document).ready(function(){ 
     $("#menubar").click(function(){ 
     $("#dropbox").animate({ 
      top: '150px', 
      height:'400px', 
     }); 
     }); 
    }); 

    </script> 

<body> 
    <div id="dropbox"> 

    </div> 
</body> 

然而,當我將我的JS代碼到一個文件名爲aboutme.js和評論出了標題中的JS代碼,它不起作用。我aboutme.js文件看起來像這樣

window.onload = pageLoad; 

function pageLoad() { 
    $("#dropbox").click(function(){ 
    $("#dropbox").animate({ 
     height:'400px', 
    }); 
    }); 
}; 

但是,代碼工作,如果我在HTML頭離開。任何理由?謝謝!

+1

請在瀏覽器錯誤控制檯中尋找第一個發現您自己的編碼錯誤的地方。你真的不應該來這裏發現已經在錯誤控制檯中解釋過的簡單錯誤。 – jfriend00 2014-10-16 23:20:23

+0

將你自定義的'

0

您需要包含JS文件的順序,以便依賴於其他文件的文件應包含在其他文件中。

所以,如果你添加aboutme的jQuery後,它應該工作

<head> 
    <link href = "css/bootstrap.min.css" rel="stylesheet"> 
    <link href = "css/aboutmestyle.css" rel="stylesheet"> 


    <script src = "http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"> 
    <script src= "js/aboutme.js" type="text/javascript"></script> // add aboutme here 
    </script> 
    <script src= "js/bootstrap.js"> 
    </script> 

    <script type="text/javascript"> 
    $(document).ready(function(){ 
     $("#menubar").click(function(){ 
     $("#dropbox").animate({ 
      top: '150px', 
      height:'400px', 
     }); 
     }); 
    }); 

     </script> 
    <body> 
     <div id="dropbox"> 
     </div> 
    </body> 
0

移動的代碼行:

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

下面這行代碼:

<script src = "http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"> 
</script> 

它應該看起來像這樣:

<head> 
    <link href = "css/bootstrap.min.css" rel="stylesheet"> 
    <link href = "css/aboutmestyle.css" rel="stylesheet"> 

    <script src = "http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"> 
    </script> 
    <script src= "js/aboutme.js" type="text/javascript"></script> 
    <script src= "js/bootstrap.js"> 
    </script> 
相關問題