javascript - Jquery点击函数,同一个按钮显示不同的文字

标签 javascript jquery

大家好,我正在尝试使用点击功能。当用户单击按钮时,将出现第二个文本,第一个文本将不显示,然后用户再次单击同一按钮,这次将出现第三个文本,第二个文本将出现将显示,最后再次单击同一按钮,将出现第四个文本,第三个文本不显示。这是我的功能:

$("#slider1next").click(function () {
    $(".text").css('display', '');
    $("#first_one").css('display','none');                   
});

这是 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>​

您还可以在那里看到http://jsfiddle.net/ganymedes/7kxAE/

最佳答案

$("#slider1next").click(function () {
    $(".text:visible").hide().next().show();
});

换句话说,每次点击,您 hide :visible文本,以及show next一个。

<强> DEMO

如果您想在到达最后一个 p 时循环回到第一个,请使用以下命令:

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

<强> DEMO 。 ​

关于javascript - Jquery点击函数,同一个按钮显示不同的文字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12989036/

相关文章:

jquery 如果 url 第一个路径名与 href 匹配,则

javascript 匿名函数

JavaScript 数字验证函数

javascript - ScrollHeight 未使用 JQuery 在 IE8 中更新

javascript - 如何将 Bootstrap 表单助手国家/地区选择器插入表单?

javascript - 弹出窗口关闭不适用于 iframe 内容

javascript - 我如何知道 V8 中有哪些功能?

javascript - AJAX 将 jQuery 发布到 PHP $_POST

javascript - Chrome Native Messaging API chrome.runtime.connectNative 不是函数

php 和 jquery 图像选择器