2012-07-17 126 views
1

我在我的HTML文件中有兩個$(document).ready(function($)函數。現在因爲有兩個ready()函數,只有一個執行!由於我對jQuery沒有太多的想法,所以我無法將它們合併。在jQuery中合併兩個ready()函數

<script type="text/javascript"> 

    $(document).ready(function($){ 

     $('#mega-1').dcVerticalMegaMenu({ 
      rowItems: '3', 
      speed: 'fast', 
      effect: 'show', 
      direction: 'right' 
     }); 
     $('#mega-2').dcVerticalMegaMenu({ 
      rowItems: '3', 
      speed: 'slow', 
      effect: 'fade', 
      direction: 'left' 
     }); 
     $('#mega-3').dcVerticalMegaMenu({ 
      rowItems: '4', 
      speed: 'slow', 
      effect: 'slide', 
      direction: 'right' 
     }); 
     $('#mega-4').dcVerticalMegaMenu({ 
      rowItems: '3', 
      speed: 'fast', 
      effect: 'slide', 
      direction: 'left' 
     }); 

    }); 
    </script> 

jQuery的功能2:

<script type="text/javascript"> 
    $(document).ready(function(){ 
     $("#featured > ul").tabs({fx:{opacity: "toggle"}}).tabs("rotate", 5000, true); 
    }); 
</script> 
+0

剛剛從第二就緒()函數的第一個添加語句。 – 2012-07-17 11:24:43

+0

JS是否必須內聯?你可以將它移動到一個外部JS文件並引用它? – nitsua 2012-07-17 11:25:01

+5

有些事情是錯誤的,因爲所有準備就緒的函數都應該觸發......我已經在整個地方使用了大量文檔準備好的函數而沒有問題。 – 2012-07-17 11:25:14

回答

1
<!DOCTYPE html> 
<html> 
<head> 
<meta charset=utf-8 /> 
<title>Page Title</title> 

<script src="some_javascript_file.js"></script> 
<script src="another_js_file.js"></script> 

</head> 
<body> 

<!-- HTML HERE --> 


<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script> 
<script src="jQuery_plugin_1.js"></script> 
<script src="jQuery_plugin_2.js"></script> 
<script> 
(function($){ // remap $ to jQuery 

    $(function(){ 
     $('#mega-1').dcVerticalMegaMenu({ 
      rowItems: '3', 
      speed: 'fast', 
      effect: 'show', 
      direction: 'right' 
     }); 
     $('#mega-2').dcVerticalMegaMenu({ 
      rowItems: '3', 
      speed: 'slow', 
      effect: 'fade', 
      direction: 'left' 
     }); 
     $('#mega-3').dcVerticalMegaMenu({ 
      rowItems: '4', 
      speed: 'slow', 
      effect: 'slide', 
      direction: 'right' 
     }); 
     $('#mega-4').dcVerticalMegaMenu({ 
      rowItems: '3', 
      speed: 'fast', 
      effect: 'slide', 
      direction: 'left' 
     }); 


     // ===================================== 

     $("#featured > ul").tabs({fx:{opacity: "toggle"}}).tabs("rotate", 5000, true); 

    }); 

})(jQuery); 
</script> 

</body> 
</html>