2014-02-18 89 views
0

我對jquery很新,想知道如何結合jquery函數。現在,我的代碼有點亂!我已經做了一些研究,並且在組合函數時無法保持正常運行。我不想讓我的代碼看起來像這樣,但至少現在它工作正常(Firefox除外)。結合功能的最佳做法是什麼?將多個jquery函數合併爲一個函數

<script type="text/javascript"> 
$(document).ready(function() { 
    jQuery(function(){ 
     jQuery("#music").click(function() { 
     jQuery("#musicinfo").slideToggle('slow'); 
     jQuery("#fpinfo, #behindinfo, #behindinfo, #signupinfo").hide('slow'); 

     }); 
    }); 
    }); 
</script> 





<script type="text/javascript"> 
$(document).ready(function() { 
    jQuery(function(){ 
     jQuery("#fproduct").click(function() { 
     jQuery("#fpinfo").slideToggle('slow'); 
     jQuery("#musicinfo, #behindinfo, #behindinfo, #signupinfo").hide('slow'); 

     }); 
    }); 
    }); 
</script> 




<script type="text/javascript"> 
$(document).ready(function() { 
    jQuery(function(){ 
     jQuery("#behind").click(function() { 
     jQuery("#behindinfo").slideToggle('slow'); 
     jQuery("#fpinfo, #musicinfo, #signupinfo").hide('slow'); 

     }); 
    }); 
    }); 
</script> 

    <script type="text/javascript"> 
    $(document).ready(function() { 
    jQuery(function(){ 
     jQuery(".exit").click(function() { 
     jQuery("#behindinfo, #musicinfo, #fpinfo, #signupinfo").hide('slow'); 

     }); 
    }); 
    }); 
</script> 

<script type="text/javascript"> 
$(document).ready(function() { 
    jQuery(function(){ 
     jQuery("#signup").click(function() { 
     jQuery("#signupinfo").slideToggle('slow'); 
     jQuery("#fpinfo, #musicinfo, #behindinfo").hide('slow'); 

     }); 
    }); 
    }); 
</script> 

    <script type="text/javascript"> 
    $(document).ready(function() { 
    jQuery(function(){ 
     jQuery(".exit").click(function() { 
     jQuery("#behindinfo, #musicinfo, #fpinfo, #signupinfo").hide('slow'); 

     }); 
    }); 
    }); 
</script> 



<script> 
    $('#behind, #fproduct, #music, #signup').click(function(){ 
     var divLoc = $('#top').offset(); 
     $('html, body').animate({scrollTop: divLoc.top}, "slow"); 
    }); 
</script> 
+2

這是HTTPS更適合://代碼審查.stackexchange.com/我想。 – Andy

+0

它聽起來不像你想要的功能。看起來你正在試圖結合腳本塊。簡單的答案是,您可以通過將所有代碼複製到一個塊就緒功能來完成。至於代碼應該如何佈局,codereview確實是一個好地方 –

回答

0

類似的東西:

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

     jQuery("#music").click(function() { 
     jQuery("#musicinfo").slideToggle('slow'); 
     jQuery("#fpinfo, #behindinfo, #behindinfo, #signupinfo").hide('slow'); 

     }); 


     jQuery("#fproduct").click(function() { 
     jQuery("#fpinfo").slideToggle('slow'); 
     jQuery("#musicinfo, #behindinfo, #behindinfo, #signupinfo").hide('slow'); 

     }); 

     jQuery("#behind").click(function() { 
     jQuery("#behindinfo").slideToggle('slow'); 
     jQuery("#fpinfo, #musicinfo, #signupinfo").hide('slow'); 

     }); 

     jQuery(".exit").click(function() { 
     jQuery("#behindinfo, #musicinfo, #fpinfo, #signupinfo").hide('slow'); 

     }); 

     jQuery("#signup").click(function() { 
     jQuery("#signupinfo").slideToggle('slow'); 
     jQuery("#fpinfo, #musicinfo, #behindinfo").hide('slow'); 

     }); 

     jQuery(".exit").click(function() { 
     jQuery("#behindinfo, #musicinfo, #fpinfo, #signupinfo").hide('slow'); 

     }); 

    $('#behind, #fproduct, #music, #signup').click(function(){ 
     var divLoc = $('#top').offset(); 
     $('html, body').animate({scrollTop: divLoc.top}, "slow"); 
    }); 
    }); 


</script> 
+0

真棒,非常感謝! – user3216873

+0

不客氣。您也可以將「JQuery」替換爲「$」。 –

0

結合並堅持使用$符號的jQuery後,我們得到這樣的:

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

    $("#music").click(function() { 
      $("#musicinfo").slideToggle('slow'); 
      $("#fpinfo, #behindinfo, #behindinfo, #signupinfo").hide('slow'); 
      }); 
    $("#behind").click(function() { 
     $("#behindinfo").slideToggle('slow'); 
     $("#fpinfo, #musicinfo, #signupinfo").hide('slow'); 
      }); 
    $("#fproduct").click(function() { 
     $("#fpinfo").slideToggle('slow'); 
     $("#musicinfo, #behindinfo, #behindinfo, #signupinfo").hide('slow'); 
      }); 
    $(".exit").click(function() { 
     $("#behindinfo, #musicinfo, #fpinfo, #signupinfo").hide('slow'); 
      }); 
    $("#signup").click(function() { 
     $("#signupinfo").slideToggle('slow'); 
     $("#fpinfo, #musicinfo, #behindinfo").hide('slow'); 

      }); 
     $(".exit").click(function() { 
     $("#behindinfo, #musicinfo, #fpinfo, #signupinfo").hide('slow'); 

     }); 

    $('#behind, #fproduct, #music, #signup').click(function(){ 
     var divLoc = $('#top').offset(); 
     $('html, body').animate({scrollTop: divLoc.top}, "slow"); 
    }); 
}); 
</script> 
相關問題