javascript - 下一个和上一个按钮功能不起作用

标签 javascript jquery tabs

我创建了具有下一个和上一个按钮功能的选项卡内容。但下一个和上一个按钮功能不起作用。我使用过 jquery.min.js 版本 1.11.1 版本。我创建了如下代码片段。我已经提到了this question并创建了答案中提到的功能。但仍然不起作用。

$("ul.kyc-tab-list li a").click(function () {
        $("ul.kyc-tab-list li a").removeClass("active");
        $(this).addClass("active");
    });

    $(".tab-btn1").click(function () {
        $("#tab1").show();
        $("#tab2").hide();
        $("#tab3").hide();
        $("#tab4").hide();
        $("#tab5").hide();
    });

    $(".tab-btn2").click(function () {
        $("#tab1").hide();
        $("#tab2").show();
        $("#tab3").hide();
        $("#tab4").hide();
        $("#tab5").hide();
    });

    $(".tab-btn3").click(function () {
        $("#tab1").hide();
        $("#tab2").hide();
        $("#tab3").show();
        $("#tab4").hide();
        $("#tab5").hide();
    });

    $(".tab-btn4").click(function () {
        $("#tab1").hide();
        $("#tab2").hide();
        $("#tab3").hide();
        $("#tab4").show();
        $("#tab5").hide();
    });

    $(".tab-btn5").click(function () {
        $("#tab1").hide();
        $("#tab2").hide();
        $("#tab3").hide();
        $("#tab4").hide();
        $("#tab5").show();
    });
    
    
    $('#btnNext').click(function () {


        // get current tab

        var currentTab = $(".tab.active");

        // get the next tab, if there is one
        var newTab = currentTab.next();

        // at the end, so go to the first one
        if (newTab.length === 0) {
            newTab = $(".tab").first();
        }

        currentTab.removeClass('active');
        // add active to new tab
        newTab.addClass('active');
    });

    $('#btnPrevious').click(function () {
        // get current tab
        var currentTab = $(".tab.active");

        // get the previous tab, if there is one
        var newTab = currentTab.prev();

        // at the start, so go to the last one
        if (newTab.length === 0) {
            newTab = $(".tab").last();
        }

        currentTab.removeClass('active');
        // add active to new tab
        newTab.addClass('active');
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div>

<section>

  <div class="kyc-tab-wrapper">
                      <ul class="kyc-tab-list">
                        <li>
                            <a href="javascript:void(0);" class="tab active tab-btn1"><span>01</span>Customer</a>
                        </li>
                        <li>
                            <a href="javascript:void(0);" class="tab tab-btn2"><span>02</span>Contact / Promotors</a>
                        </li>
                        <li>
                            <a href="javascript:void(0);" class="tab tab-btn3"><span>03</span>Bank / Business</a>
                        </li>
                        <li>
                            <a href="javascript:void(0);" class="tab tab-btn4"><span>04</span>Other</a>
                        </li>
                        <li>
                            <a href="javascript:void(0);" class="tab tab-btn5"><span>05</span>Finish</a>
                        </li>
                    </ul>
                    
                       <div class="kyc-tab-content" id="tab1">
                       
                       tab 1
                       </div>
                                <div class="kyc-tab-content" id="tab2">
                        tab 2
                       </div>         
                                </div>
                                
                                     <div class="kyc-tab-content" id="tab3">
                                         tab 3
                       
                                     </div>
                                                  <div class="kyc-tab-content" id="tab4">
                                                  tab 4
                                                  </div>
                                                       <div class="kyc-tab-content" id="tab5">
                                   
                                   tab 5</div>
                                                       
                                                         <div class="kyc-tab-form-btn-wrap">
                        <button id="btnPrevious" style="display:none">  Previous</button> 
                        <button id="btnNext">Next </button>
                    </div>
                       </div>
  </div>
</section>


</div>

最佳答案

要使 Next 按钮正常工作,请更改以下行,如下所示。

// get current tab
var currentTab = $(".tab.active").closest("li");
//get the next tab, if there is one
var newTab = currentTab.next().find("a.tab");
//...
currentTab.find(".tab").removeClass('active');

Prev 按钮也进行类似的更改。

// get current tab
var currentTab = $(".tab.active").closest("li");
// get the previous tab, if there is one
var newTab = currentTab.prev().find("a.tab");
//...
currentTab.find(".tab").removeClass('active');

<强> Fiddle Demo

代码中的问题是您没有正确关注下一个或上一个 a 标记。

关于javascript - 下一个和上一个按钮功能不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45912489/

相关文章:

javascript - ajax请求周期自动刷新时如何使用knockout.js数据绑定(bind)?

php - 从javascript调用php文件

javascript - JavaScript 中是否有一些语音或语音就绪事件?

javascript - Eloqua 替换 &lt;script&gt; 标签

javascript - 显示 Bootstrap 下拉菜单以将列表定向到左侧而不是右侧

javascript - 打开或关闭 Accordion /选项卡时的样式页面

javascript - 多选onchange事件jquery

javascript - 当我点击时 jQuery 没有返回正确的 ID

标签的android字体大小

css - 如何通过 css 排除一个特定的类?