2014-02-21 13 views
1

這是片段,我需要它們放入一個公共文件。這個想法是有一個extern .js文件與所有這些片段我試圖複製一個文件中的所有代碼,但它不起作用。是否有可能有一個以上的$(document)。就緒(函數?如何在一個文件中收集幾個小的JavaScript片段

<!-- Java Script //--> 
      <script type="text/javascript"> 
       $(".collapse").collapse() 
       $('#menu').collapse({ 
       toggle: false 
       }) 
      </script> <!-- end of navigation --> 


      <script type="text/javascript"> 

       var jump=function(e) 
       { 
        //prevent the "normal" behaviour which would be a "hard" jump 
        e.preventDefault(); 
        //Get the target 
        var target = $(this).attr("href"); 
        //perform animated scrolling 

        $('html,body').animate(
        { 
        //get top-position of target-element and set it as scroll target 
        scrollTop: $(target).offset().top 
        //scrolldelay:1 seconds 
        },1000,function() 

        { 
         //attach the hash (#jumptarget) to the pageurl 
         location.hash = target; 
        }); 
       } 


       $(document).ready(function() 
       { 
        //$('a[href*=#]').bind("click", jump); 
        $('a[href*=#]').not(document.getElementsByClassName("carousel-control")).bind("click", jump);  
        return false; 
       }); 

       </script> <!-- // end of smooth scrolling --> 


       <!-- // Shows menu after 50px --> 
       <script type="text/javascript"> 

       var fixed = false; 

       $(document).scroll(function() { 
        if($(this).scrollTop() > 25) { 
        if(!fixed) { 
          fixed = true; 
          $('#navigation').css({position:'fixed', display:'inline'}); 

         } 
        } else { 
         if(fixed) { 
          fixed = false; 
          $('#navigation').css({position:'relative', display:'block'}); 

         } 
        } 
       }); 

       </script> 

你能不能被軟件寫我,我怎麼會做呢?在此先感謝。

回答

1

你可以加入片斷在一個。文件,沒有什麼問題可以是一個問題,就是腳本執行時的那一刻,你的情況,這可能是問題:

<script type="text/javascript"> 
$(".collapse").collapse() 
$('#menu').collapse({ 
    toggle: false 
}) 
</script> <!-- end of navigation --> 

此代碼運行在它在遇到的那一刻HTML代碼,所以#menu元素必須在HTML中定義請寫下這段代碼。當你把你的代碼放在外部的JS文件中,並將它包含在HTML頭部的<頭部時,這可能不會被滿足。嘗試在

$(document).ready(function() { ... }); 
3

首先的包裝這個代碼,如果你希望把這些片段到一個單獨的文件,您應該刪除所有的腳本標記

<script type="text/javascript"> <-- Remove this 
    Keep this 
</script> <-- Remove this 
+0

哦,是的,它可能有一個以上的$(document)。就緒,但也許並不理想。請參閱此問題: http://stackoverflow.com/questions/1327756/can-you-have-multiple-document-readyfunction-sections – Culme

0

什麼控制檯會告訴你的。

也許你需要設置一些「;」在第一個片段。

1

要實現這一目標,您應該從js文件中刪除所有腳本標記。是的,你可以有多個的document.ready塊,但你可以在一個單一的document.ready塊這是一個很好的做法像包裹整個代碼片段:

 $(document).ready(function() 
     { 
      //$('a[href*=#]').bind("click", jump); 
      $('a[href*=#]').not(document.getElementsByClassName("carousel-control")).bind("click", jump);  
     $(".collapse").collapse() 
     $('#menu').collapse({ 
     toggle: false 
     }) 
     var jump=function(e) 
     { 
      //prevent the "normal" behaviour which would be a "hard" jump 
      e.preventDefault(); 
      //Get the target 
      var target = $(this).attr("href"); 
      //perform animated scrolling 

      $('html,body').animate(
      { 
      //get top-position of target-element and set it as scroll target 
      scrollTop: $(target).offset().top 
      //scrolldelay:1 seconds 
      },1000,function() 

      { 
       //attach the hash (#jumptarget) to the pageurl 
       location.hash = target; 
      }); 
     } 

     var fixed = false; 

     $(document).scroll(function() { 
      if($(this).scrollTop() > 25) { 
      if(!fixed) { 
        fixed = true; 
        $('#navigation').css({position:'fixed', display:'inline'}); 

       } 
      } else { 
       if(fixed) { 
        fixed = false; 
        $('#navigation').css({position:'relative', display:'block'}); 

       } 
      } 
     }); 

     }); 
      }); 
0

你可以有多個$(document).ready(),但它並不總是建議,檢查here

如果你正在寫的JS文件你不需要<script>標籤,如果你是你的HTML文件中寫它,那麼你需要的腳本標記

<script type="text/javascript"> 
//your JS 
</script> 

內寫,但如果你的JS的尺寸增加是最好把它放在一個單獨的文件中。

你的代碼。在設置MANNER

$(document).ready(function() 
{ 
    var fixed = false; 
    $(".collapse").collapse(); 

    $('#menu').collapse({ 
     toggle: false 
    }) 


    var jump = function(e) 
    { 
     //prevent the "normal" behaviour which would be a "hard" jump 
     e.preventDefault(); 
     //Get the target 
     var target = $(this).attr("href"); 
     //perform animated scrolling 

     $('html,body').animate(
       { 
        //get top-position of target-element and set it as scroll target 
        scrollTop: $(target).offset().top 
          //scrolldelay:1 seconds 
       }, 1000, function() 

     { 
      //attach the hash (#jumptarget) to the pageurl 
      location.hash = target; 
     }); 
    } 

    $(document).scroll(function() { 
     if ($(this).scrollTop() > 25) { 
      if (!fixed) { 
       fixed = true; 
       $('#navigation').css({position: 'fixed', display: 'inline'}); 

      } 
     } else { 
      if (fixed) { 
       fixed = false; 
       $('#navigation').css({position: 'relative', display: 'block'}); 

      } 
     } 
    }); 

    //$('a[href*=#]').bind("click", jump); 
    $('a[href*=#]').not(document.getElementsByClassName("carousel-control")).bind("click", jump); 
    return false; 
}); 

我希望它能幫助:)

相關問題