Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
152 views
in Technique[技术] by (71.8m points)

javascript - Prev/Next cycle through list where li class is active

I am trying to cycle through list with prev/next buttons. This is currently working as per the code below, however, in my application some of the list items are set to hidden unless they have a class of active which is defined by user selection.

I therefore only require the next button to apply the relevant active class to the next closest li which is also set to active, as these tabs should not be accessible until they're set to active.

Any help on this would be greatly appreciated.

$(".next").on("click", function () {
// Current selected vars
let currentActive = $("li.selected");
let currentArea = $(".area.selected");

// Remove selected from current
currentActive.removeClass("selected");
currentArea.removeClass("selected");

// Add selected to next
if (currentActive.is(":last-child")) {
  $(".tabs li").first().addClass("selected");
  $(".area").first().addClass("selected");
} else {
  currentActive.next().addClass("selected");
  currentArea.next().addClass("selected");
}});
question from:https://stackoverflow.com/questions/65920192/prev-next-cycle-through-list-where-li-class-is-active

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I have now solved this using nextAll() and then selecting the first with first().


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...