douqianni4080 2016-05-04 12:36
浏览 21
已采纳

单击按钮显示不同的文本,重复第一个文本

HTML:

<button id="slider1next" >Clickme</button>
  <p class="text" id="first_one">This is the first text</p>
    <p class="text" id="second_one" style="display:none">This is the second text</p>
    <p class="text" id="third_one" style="display:none">This is the third text</p>
<p class="text" id="fourth_one" style="display:none">This is the four text</p>

JavaScript:

$("#slider1next").click(function() {
    var $next = $(".text:visible").hide().next();
    $next.length ? $next.show() : $(".text:first").show();
});

$("#slider2next").click(function() {
    var $next = $(".text:visible").hide().prev();
    $next.length ? $next.show() : $(".text:last").show();
});

I want to make it where the text is shown, but after the last text, it repeats the first text.

Trying to make something like this

If there's a better way to do this, other than using JavaScript and HTML, please let me know.. But JavaScript and HTML is the only way I can think of, an alternative to JS functions would be jQUERY. Help? ;-;

Looking for suggestions with the use of PHP, too, maybe to mock the code's intended for what I am trying to re-create using a different set of languages.

  • 写回答

2条回答 默认 最新

  • douzha8489 2016-05-04 13:25
    关注

    var count = 0;
    $("#sliderNext").click(function() {
      count++;
      if (count < $('.text').length) {
        $(".text:nth(" + count + ")").show().prev().hide();
      } else {
        $(".text:first").show();
        $(".text:last").hide();
        count = 0;
      }
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
    <button id="sliderNext">Next</button>
    
    <p class="text" id="first_one">This is the first text</p>
    <p class="text" id="second_one" style="display:none">This is the second text</p>
    <p class="text" id="third_one" style="display:none">This is the third text</p>
    <p class="text" id="fourth_one" style="display:none">This is the four text</p>

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 vc6.0中想运行代码的时候总是提示无法打开文件是怎么回事
  • ¥25 关于##爬虫##的问题,如何解决?:
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题