简体   繁体   English

为什么此JavaScript函数不起作用?

[英]Why is this JavaScript function not working?

I am working with bootstrap and attempting to create a button in tab 1 which "activates" (switches to) tab 2. 我正在使用引导程序,并尝试在选项卡1中创建一个按钮,该按钮“激活”(切换到)选项卡2。

Here's my code: 这是我的代码:

HTML navigation tabs: HTML导航标签:

    <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: HTML标签内容:

<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: 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. 我几乎完全复制了几个示例... 单击选项卡本身效果很好 ,我似乎无法在第1页底部创建一个按钮来激活第2页。

One: bad form, inline onclick call... you don't need it, get rid of it: 一:错误的形式,内联onclick调用...您不需要它,摆脱它:

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

Two: you have two JS errors. 二:您有两个JS错误。 You didn't close your .click() function, and you're trying to trigger the first tab with the specifier of li:eq(0)... 您没有关闭.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);
});

See this jsFiddle for a working example: http://jsfiddle.net/mori57/Xr9eT/ 有关工作示例,请参见此jsFiddle: http : //jsfiddle.net/mori57/Xr9eT/

Give your button an ID : 给您的按钮一个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>

and just trigger a click on the right anchor, bootstrap does the rest : 并点击右侧锚点,其余部分将由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');
    });
});

You should not use specific code for your button, but generic event-driven process: 您不应为按钮使用特定的代码,而应使用通用的事件驱动过程:
- you should add an attribute to your button to specify towards which page it will redirect -您应该在按钮上添加一个属性,以指定它将重定向到的页面
- then attach a click event -然后附加点击事件
- attach click event to tabs to do the same -将点击事件附加到标签以执行相同操作

HTML: HTML:

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

JS to process tab switching: JS处理选项卡切换:

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

(Not sure you need to use show or hide functions if you CSS manages thats thanks to active class) (由于活动类,如果CSS管理多数民众赞成,则不确定是否需要使用显示或隐藏功能)

Then for your button: 然后为您的按钮:

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

For tabs: 对于标签:

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

And you can load page one at startup, if needed: 然后,如果需要,您可以在启动时加载第一页:

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

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM