jquery 使用下一个/上一个按钮循环遍历多个 div

标签 jquery html css

我正在使用下面的代码来遍历 div,它运行良好

$("#nextBtn").click(function() {
  var nextDiv = $(".step:visible").next(".step");
  if (nextDiv.length == 0) { // wrap around to beginning
    nextDiv = $(".step:first");
  }
  $(".step").hide();
  nextDiv.show();
});

$("#prevBtn").click(function() {
  var prevDiv = $(".step:visible").prev(".step");
  if (prevDiv.length == 0) { // wrap around to end
    prevDiv = $(".step:last");
  }
  $(".step").hide();
  prevDiv.show();
});
.step {
  display: none;
}
div.step:first-child {
  display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="form">
  <div class="step">This is step 1</div>
  <div class="step">This is step 2</div>
  <div class="step">This is step 3</div>
  <div class="step">This is step 4</div>

  <button id="prevBtn">Prev</button>
  <button id="nextBtn">Next</button>
</div>

我的问题是我将动态添加多组 div 和按钮,这会破坏如下代码

$("#nextBtn").click(function() {
  var nextDiv = $(".step:visible").next(".step");
  if (nextDiv.length == 0) { // wrap around to beginning
    nextDiv = $(".step:first");
  }
  $(".step").hide();
  nextDiv.show();
});

$("#prevBtn").click(function() {
  var prevDiv = $(".step:visible").prev(".step");
  if (prevDiv.length == 0) { // wrap around to end
    prevDiv = $(".step:last");
  }
  $(".step").hide();
  prevDiv.show();
});
.step {
  display: none;
}
div.step:first-child {
  display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.1/jquery.min.js"></script>
<div class="form">
  <div class="step">This is step 1</div>
  <div class="step">This is step 2</div>
  <div class="step">This is step 3</div>
  <div class="step">This is step 4</div>

  <button id="prevBtn">Prev</button>
  <button id="nextBtn">Next</button>
</div>
	
	
	<div class="form">
  <div class="step">This is step 5</div>
  <div class="step">This is step 6</div>
  <div class="step">This is step 7</div>
  <div class="step">This is step 8</div>

  <button id="prevBtn">Prev</button>
  <button id="nextBtn">Next</button>
</div>

我怎样才能使这项工作?因为我无法更改任何元素的类,我也不知道会有多少个元素?

最佳答案

首先,您不能将具有相同 ID 的元素添加到您的页面中,在下面的代码片段中,我将按钮的 ID 替换为类。然后,您所要做的就是调用 jquery parent() 方法以正确的形式显示/隐藏元素:)

希望对您有所帮助:)

$(".nextBtn").click(function() {
  var nextDiv = $(this).parent().find(".step:visible").next(".step");
  if (nextDiv.length == 0) { // wrap around to beginning
    nextDiv = $(this).parent().find(".step:first");
  }
  $(this).parent().find(".step").hide();
  nextDiv.show();
});

$(".prevBtn").click(function() {
  var prevDiv = $(this).parent().find(".step:visible").prev(".step");
  if (prevDiv.length == 0) { // wrap around to end
    prevDiv = $(this).parent().find(".step:last");
  }
  $(this).parent().find(".step").hide();
  prevDiv.show();
});
.step {
  display: none;
}
div.step:first-child {
  display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.1/jquery.min.js"></script>
<div class="form">
  <div class="step">This is step 1</div>
  <div class="step">This is step 2</div>
  <div class="step">This is step 3</div>
  <div class="step">This is step 4</div>

  <button class="prevBtn">Prev</button>
  <button class="nextBtn">Next</button>
</div>
	
	
	<div class="form">
  <div class="step">This is step 5</div>
  <div class="step">This is step 6</div>
  <div class="step">This is step 7</div>
  <div class="step">This is step 8</div>

  <button class="prevBtn">Prev</button>
  <button class="nextBtn">Next</button>
</div>

关于jquery 使用下一个/上一个按钮循环遍历多个 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40943251/

相关文章:

javascript - 将 jQuery 选择器用于 css,转义特殊字符

javascript - 在我的 React 应用程序中出现解析错误

javascript - jQuery 的scrollTop/scrollTo 模糊?

CSS 照片库调整帮助

html - 如何在 ionic 5 中自定义 ionic 范围?

html - Wordpress 帖子网格图像在分辨率高于 1280 时留下灰色空间

javascript - js排序时保留选中项

javascript - jQuery 1.5.2 获取全屏事件 Chrome

jquery - 先进的 ASP.NET 提高性能

html - 为所有其他 css 类使用通用 css 样式的方法