javascript - 单击按钮后如何逐个调用部分并且上一节将隐藏?

标签 javascript jquery html css

我有 3 个部分和一个按钮

  1. 当用户第一次点击按钮时,应该显示第一个section的内容。
  2. 当用户第二次点击同一个按钮时,应显示第二部分并隐藏第一部分。
  3. 第三部分也是如此。

请帮助我。

<section id="one" style="display:none;">
    <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,</p>
</section>

<section id="two" style="display:none;">
    <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p>
</section>

<section id="three" style="display:none;">
    <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s</p>
</section>

<input type="submit" onclick="checkDiv();">

<script>
    function checkDiv(){
        document.getElementById('one').style.display = "block";
    }
</script>

最佳答案

如果您将 jquery 包含到您的元素中,您可以这样做:

function next() {
  var current = $('.current');
  var next = current.next('.m-div:first');
  
  // loop back to the first div
  if(next.length == 0) { next = $('#one'); }
  
  current.removeClass('current');
  next.addClass('current');
}
.m-div {display: none;}

.m-div.current {
  display: block;
 }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section class="m-div current" id="one" >
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,</p>
</section>

<section class="m-div" id="two">
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p>
</section>


<section class="m-div" id="three">
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s</p>
</section>

<input type="submit" onclick="next();">

关于javascript - 单击按钮后如何逐个调用部分并且上一节将隐藏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37322880/

相关文章:

javascript递归变量问题

javascript - Json 转储到单独的变量

javascript - 到底什么才算内联 JavaScript? (回顾速度/效率缓存比较)

javascript - Bootstrap 轮播控件没有响应

javascript - Qualtrics 中的嵌套循环

c# 将 POST 中的字符串从 jquery 传递到 WebAPI 项目方法时出错

jQuery 在输入文本上显示空字符串?

jquery - 如何禁用 JQuery 选项卡上的单击事件?

html - 无法在特定尺寸的 Canvas 上绘制

javascript - 如何将css放在angular js中的选定行中?