문제

how to shorten the code? He wants to apply the principle of DRY? Here code:

$('a[href^="#register"]').click(function(e){
    $('.box').addClass('active');
    $('.box div').load('register.html');
    e.preventDefault();
});

$('a[href^="#blog"]').click(function(e){
    $('.box').addClass('active');
    $('.box div').load('blog.html');
    e.preventDefault();
});

$('a[href^="#contact"]').click(function(e){
    $('.box').addClass('active');
    $('.box div').load('contact.html');
    e.preventDefault();
});

Please help :)

도움이 되었습니까?

해결책 2

apply class on anchor tags and write event on class like this:

$('a.MyClass').click(function(e){


$('.box').addClass('active');
$('.box div').load($(this).attr("href").split('#')[1]+'.html');
e.preventDefault();


});

다른 팁

Try something like:

$('a[href^="#register"], a[href^="#blog"], a[href^="#contact"]').click(function(e){
    $('.box').addClass('active');
    $('.box div').load(this.getAttribute('href').split('#')[1]+'.html');
    e.preventDefault();
});
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top