0

I am working with bootstrap and attempting to create a button in tab 1 which "activates" (switches to) tab 2.

Here's my code:

HTML navigation tabs:

    <ul id="pageSwitcher" class="nav nav-tabs" style="width:100%;">
      <li><a href="#page1" data-toggle="tab">Page One</a></li>
      <li><a href="#page2" data-toggle="tab">Page Two</a></li>
    </ul>

HTML tab content:

<div class="tab-content" style="width:100%;">
    <div class="tab-pane active" id="page1">
        <button type="button" onclick="showPageTwo();">Proceed to page 2</button>
    </div>
    <div class="tab-pane" id="page2">
        <p>Page 2 content here!</p>
    </div>
</div>

JavaScript:

<script type="text/javascript">

$(document).ready(function() {
    $('#myTab a').click(function (e) {
        e.preventDefault();
        $(this).tab('show');
    }
});

function showPageTwo() {
    $('#pageSwitcher li:eq(1) a').tab('show');
}

</script>

Anyone willing to provide some insight as to where exactly I'm going wrong? I've copied several examples almost exactly... clicking on the tabs themselves works fine, I just can't seem to make a button at the bottom of page 1 that activates page 2.

4

4 回答 4

2

一:形式不好,内联onclick调用……你不需要,去掉它:

        <button type="button">Proceed to page 2</button>

二:你有两个 JS 错误。您没有关闭 .click() 函数,而是尝试使用 li:eq(0) 的说明符触发第一个选项卡...

$(document).ready(function() {
    $('#myTab a').click(function (e) {
        e.preventDefault();
        $(this).tab('show');
        // ALSO, you were missing a closing paren, here!
    });

    //two issues ... onclick inside your button is trying to access
    // your function before it's available... inline js like that, bad idea anyhow
    // so hook into it inside your DOM ready code here anyhow.

    var showPageTwo = function() {
        // secondly, you're looking at the wrong item!
        // li:eq(0) means "look at the first li in the array of li"
        $('#pageSwitcher li:eq(1) a').tab('show');
    };

    $(".tab-content").on("click","#page1 button", showPageTwo);
});

有关工作示例,请参阅此 jsFiddle:http: //jsfiddle.net/mori57/Xr9eT/

于 2013-05-22T14:51:26.310 回答
1

给你的按钮一个 ID:

<div class="tab-content" style="width:100%;">
    <div class="tab-pane active" id="page1">
        <button type="button" id="myButton">Proceed to page 2</button>
    </div>
    <div class="tab-pane" id="page2">
        <p>Page 2 content here!</p>
    </div>
</div>

只需点击正确的锚点,bootstrap 就会完成剩下的工作:

$(document).ready(function() {
    $('#myTab a').on('click', function(e) {
        e.preventDefault();
        $(this).tab('show');
    });

    $('#myButton').on('click', function() {
        $('.nav-tabs li:eq(1) a').trigger('click');
    });
});
于 2013-05-22T14:43:28.700 回答
0

您不应该为您的按钮使用特定代码,而是使用通用事件驱动过程:
- 您应该为您的按钮添加一个属性以指定它将重定向到哪个页面
- 然后附加一个点击事件
- 将点击事件附加到选项卡以执行相同的操作

HTML:

<button type="button" id="btn" gotoPage="#page2">Proceed to page 2</button>

JS处理tab切换:

function showPage(pageId){
    $('.tab-pane').each(function(){
        $(this).removeClass('active');
        $(this).hide();
    }); 
    $(pageId).show();
    $(pageId).addClass('active');
 }

(不确定是否需要使用显示或隐藏功能,如果您的 CSS 管理这要归功于活动类)

然后为您的按钮:

$('#btn').click(function(){
    var destPage = $(this).attr('gotoPage');
    showPage(destPage);
});

对于标签:

$('#pageSwitcher a').each(function(){
    $(this).click(function(e){
        showPage($(this).attr('href'));
    });
});

如果需要,您可以在启动时加载第一页:

$(document).ready(function(){
    showPage("#page1");    // default: load page 1
});

示例:http: //jsfiddle.net/DSbrj/1/

于 2013-05-22T15:24:31.950 回答
-1

showPageTwo() 应该在 document.ready() 调用之外声明。

于 2013-05-22T14:38:47.977 回答