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


Click button show different text, repeat first text

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();
});

我想把它放在显示文本的地方,但在最后一个文本之后,它会重复第一个文本。

尝试制作类似的东西

如果有比使用JavaScript和HTML更好的方法,请告诉我。。但是JavaScriptHTML是我唯一能想到的方法,JS函数的替代方法是jQUERY。帮助?;-;

也在寻找使用PHP的建议,也许是为了模拟我试图使用不同语言重新创建的代码。

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>

使用模函数循环通过一组数字:(count%4)+1。如果计数从0开始,并随着每次点击下一个按钮而增加,则会在数字1、2、3和4之间循环。