移动到外部.js文件时JavaScript无法正常工作

时间:2012-07-02 07:14:16

标签: javascript jquery html css

我在html页面中使用过java脚本。现在我将脚本移动到外部js文件中。 这是脚本(jful.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文档时,它可以工作!(当我们向下滚动页面时执行此脚本)问题是什么?

3 个答案:

答案 0 :(得分:6)

删除这些

<script type="text/javascript">

</script>

答案 1 :(得分:5)

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

答案 2 :(得分:1)

如果您已将<script type="text/javascript">添加到JS文件中删除标记,则应该只将纯JS代码包含在.js文件中。