2013-07-16 4 views
0

링크를 클릭했을 때에 내가 부하를 다음 코드에 jQuery를 코드 : 나는 또한 다른로드 기능 insde 수 있도록이 코드를 원하는하나 개 이상의 기능

$('.ops .menu .panel a').on('click',function(){ 
         $('.ops .lay').css('opacity',0.1).load($(this).attr('href'), function(){ $('.ops .lay').css('opacity',1) }); 
         $(this).parent().parent().find('a').removeClass('active'); $(this).addClass('active'); 
         return false 
        }); 

. 나는 그것을 반복해야합니까, 아니면 다른 방법이 있습니까? 그것은 어딘가에 저장할 수 있습니까?

답변

2
function act(){ 
    $('.ops .lay').css('opacity',0.1).load($(this).attr('href'), function(){ $('.ops .lay').css('opacity',1) }); 
    $(this).parent().parent().find('a').removeClass('active'); $(this).addClass('active'); 
    return false 
}; 

$('.ops .menu .panel a').on('click', act); 

사용도 다른 곳 act 방법 :

4

저장 변수로 기능과 두 곳에서 참조 :

var myFunction = function() { 
    $('.ops .lay').css('opacity',0.1).load($(this).attr('href'), function(){ 
     $('.ops .lay').css('opacity',1) 
    }); 

    $(this).parent().parent().find('a').removeClass('active'); 
    $(this).addClass('active'); 
    return false; 
}; 

$('.ops .menu .panel a').on('click', myFunction); 

// i assume by "load function" you meant document ready, but you get the idea 
$(document).ready(myFunction); 
관련 문제