Question

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?

Était-ce utile?

La solution 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 :)

Autres conseils

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);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top