简体   繁体   中英

add class to next element jquery click function

I have created a vertical slider and I want the classes to move onto the next div on click (next) and previous on click (prev)

here is my code and fiddle

$(".bxslider-inner:nth-child(4n+1)").addClass('noBlur');
$(".bxslider-inner:nth-child(4n+2)").addClass('Blur1');
$(".bxslider-inner:nth-child(4n+3)").addClass('Blur2');
$(".bxslider-inner:nth-child(4n)").addClass('Blur3');

$("a.bx-next").click(function(){
   $(".bxslider-inner:nth-child(4n+1)").next().addClass('noBlur');
$(".bxslider-inner:nth-child(4n+2)").next().addClass('Blur1');
$(".bxslider-inner:nth-child(4n+3)").next().addClass('Blur2');
$(".bxslider-inner:nth-child(4n)").next().addClass('Blur3');
});


$("a.bx-prev").click(function(){
   $(".bxslider-inner:nth-child(4n+1)").prev().addClass('noBlur');
$(".bxslider-inner:nth-child(4n+2)").prev().addClass('Blur1');
$(".bxslider-inner:nth-child(4n+3)").prev().addClass('Blur2');
$(".bxslider-inner:nth-child(4n)").prev().addClass('Blur3');
});

Classes seem to be colliding with each other. I'd suggest cleaning current classes before adding the 'blur' classes, eg :

$(".bxslider-inner:nth-child(4n+1)").next().removeClass().addClass('bxslider-inner').addClass('noBlur');

etc... Problem is it only works for he first click on the button, as

$(".bxslider-inner:nth-child(4n+1)").next()

Will always be the same element. You now need to find a way to fetch the right elements on your click function.

Some elements here : In bxslider i want to add a class on current slide

它们似乎与DOM树处于同一级别,因此您可以使用:

$(this).next().click();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM