質問

The following code I have load on when a link is clicked:

$('.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
                    });

I want this code to also be insde another load function. Do I have to repeat it or is there another way. Can it be stored somewhere?

役に立ちましたか?

解決 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);

use act method somewhere else too :)

他のヒント

Save the function as a variable and reference it from both places:

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);
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top