2013-03-21 81 views
0

試圖理解這個jQuery,我發現了2個腳本我想使用,但我可以將它們合併爲1嗎?目前我有他們都在不同的腳本標籤。我如何將2 jQuery合併爲1

這是第一個腳本

$(function(){ 
     $('#navigation_horiz').delay(100).fadeIn(100); 
     $('#navigation_horiz').naviDropDown({ 
     dropDownWidth: 'auto', //the default width of drop down elements 
     slideDownDuration: 600, //easing duration for slideDown 
     slideUpDuration: 300 //easing duration for slideUp  
     }); 
}); 

這裏是第二個腳本

if (franchise_id==undefined) { //hide commish nav is not signed into site 
    var franchise_id; 
    document.write("<style type='text/css'>"); 
    document.write("#navigation_horiz ul li.commish { display: none; }"); 
    document.write("</style>"); 
} 
else if (franchise_id=="0000") { } //show commish nav is logged in as commish 
else { // hide both if logged in as any other franchise 
    document.write("<style type='text/css'>"); 
    document.write("#navigation_horiz ul li.commish { display: none; }"); 
    document.write("</style>"); 
} 

試圖把在同一...下面,而不是在例如所有

$(function(){ 
     $('#navigation_horiz').delay(100).fadeIn(100); 
     $('#navigation_horiz').naviDropDown({ 
     dropDownWidth: 'auto', //the default width of drop down elements 
     slideDownDuration: 600, //easing duration for slideDown 
     slideUpDuration: 300 //easing duration for slideUp  
     }); 
}); 
if (franchise_id==undefined) { //hide commish nav is not signed into site 
    var franchise_id; 
    document.write("<style type='text/css'>"); 
    document.write("#navigation_horiz ul li.commish { display: none; }"); 
    document.write("</style>"); 
} 
else if (franchise_id=="0000") { } //show commish nav is logged in as commish 
else { // hide both if logged in as any other franchise 
    document.write("<style type='text/css'>"); 
    document.write("#navigation_horiz ul li.commish { display: none; }"); 
    document.write("</style>"); 
} 
}); 
+1

是的,是的,你可以。 :) – adamb 2013-03-21 14:54:16

+0

任何你不能把它們放在同一腳本標記下的原因?您並不僅限於您知道的每個標籤的一項功能。 – DiMono 2013-03-21 14:55:03

+0

順便說一句,第二個是不是甚至jQuery – 2013-03-21 15:02:22

回答

0
工作

您可以將它們放入單個標籤中,但將它們排除在單獨的.js文件中並在最後加載它會更好你的文件。

+0

編輯我的第一篇文章,試圖結合,但它不會工作,不知道如何創建一個自己的js文件 – 2013-03-21 15:15:00

2

是的,你可以。第一部分是jQuery,第二部分是原始Javascript,全部在頂級命名空間下。

唯一需要注意的是:jQuery代碼依賴於jQuery lib和一個獨立的插件(naviDropDown?)。因此,您必須在 jQuery和插件之後加上合併代碼

0

函數只能執行好,執行它。

如果您在第一次加載網站時加載第二個腳本,如果您不打算在加載時調用該功能,請不要將第二個腳本放入函數中。

只有當它們服務於同一事件時,您才應該將腳本合併到相同的函數中。