2012-07-02 46 views
0

我在我的html頁面中工作了一個java腳本。現在我將腳本移動到外部js文件。 這裏是腳本(jful.js)JavaScript移動到外部.js文件時不工作

<script type="text/javascript"> 
$(function() { 

    // grab the initial top offset of the navigation 
    var sticky_navigation_offset_top = $('#catnav').offset().top; 

    // our function that decides weather the navigation bar should have "fixed" css position or not. 
    var sticky_navigation = function(){ 
     var scroll_top = $(window).scrollTop(); // our current vertical position from the top 

     // if we've scrolled more than the navigation, change its position to fixed to stick to top, 
     // otherwise change it back to relative 
     if (scroll_top > sticky_navigation_offset_top) { 
      $('#catnav').css({ 'position': 'fixed', 'top':0, 'left':0 }); 
     } else { 
      $('#catnav').css({ 'position': 'relative' }); 
     } 
    }; 

    // run our function on load 
    sticky_navigation(); 

    // and run it again every time you scroll 
    $(window).scroll(function() { 
     sticky_navigation(); 
    }); 

}); 
</script> 

現在腳本不能正常工作。 包括我這樣的

<head><script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script> 
<script type="text/javascript" src="jful.js"></script></head> 

腳本當我再次直接添加腳本的HTML文檔,它的工作原理!(這個腳本是,當我們向下滾動頁面執行)這是什麼問題?

+1

請添加您收到的錯誤消息或關閉您的問題,因爲人們只能猜測出現了什麼問題,然後他們猜錯了,然後您說不工作,然後他們再次猜測,那又錯了,等等。附:這可能不起作用,因爲最初的錯誤文件是從緩存中獲取的 – Esailija

+0

對不起,我不會再重複一遍。 –

回答

6

刪除這些

<script type="text/javascript"> 

</script> 
+0

已刪除,但無法正常工作。 –

+2

您是否檢查了您的js路徑。 – Prashobh

+0

@AshwinSingh他已經調用了=>'$(function(){' – undefined

5

你不需要在jful.js. <script type="text/javascript"></script>

1

如果您已將<script type="text/javascript">添加到您的JS文件中,請刪除標籤,您應該將純JS代碼包含在.js文件中。

相關問題