简体   繁体   English

通过javascript链接href以单击选项卡中的按钮

[英]link href by javascript to click a button in a tab

There is a (Bootstrap)website like below: 有一个(Bootstrap)网站,如下所示:

<ul id="myTab">
    <li><a href="#tab1">TAB1</a></li>
    <li><a href="#tab2">TAB2</a></li>
    <li><a href="#tab3">TAB3</a></li>
</ul>

<div id="myTabContent">
    <div id="tab1">
        <!--tab1 content-->
    </div>
    <div id="tab2">
        <div>
            <button id="button1">BUTTON1</button>
            <button id="button2">BUTTON2</button>
            <button id="button3">BUTTON3</button>
        </div>
    </div>
    <div id="tab3">
        <!--tab3 content-->
    </div>
</div>

Here is my question, when I use a link to button3 in tab2: 这是我的问题,当我使用tab2中的button3链接时:

<a href="javascript: 
    $('#myTab li:eq(1) a').tab('show');
    $('#button3').click();
    $('html,body').scrollTop(0);">GoToButton3Tab2
</a>

It works,though; 虽然有效, But I don't want to see the page first to tab2 and then to button3. 但我不想先看到该页面的tab2,然后再看到button3。

How can I improve my code so that can go to button3 in tab 2 directly? 如何改善我的代码,以便可以直接转到选项卡2中的button3?

Here is my problem code in fiddle: http://jsfiddle.net/vincida/o0rb09ry/34/ 这是我的小提琴中的问题代码: http : //jsfiddle.net/vincida/o0rb09ry/34/

But the content when clicking button is from ajax, and I don't know how to write ajax in fiddle, so it is replaced by iframe. 但是单击按钮时的内容来自ajax,而且我不知道如何用小提琴编写ajax,因此将其替换为iframe。

In fiddle, it doesn't show the situation what I faced. 摆弄,它没有显示我所面对的情况。

In fiddle, when I press "GoToButton3Tab2" in tab1,the website goes to button3 of tab2 directly. 摆弄时,当我在tab1中按“ GoToButton3Tab2”时,网站直接转到tab2的button3。

In my situation, when I press "GoToButton3Tab2" in tab1, the website always pass through content of button1 of tab2, and then goes to button3 of tab2. 在我的情况下,当我在tab1中按下“ GoToButton3Tab2”时,网站总是通过tab2的button1的内容,然后再转到tab2的button3。

I guess that is because I set button3 of tab2 to active when window.onload 我猜这是因为在window.onload时将tab2的button3设置为活动

so I solve it by replacing 所以我通过替换来解决

window.onload = function() {
    document.getElementById("button1").click();
}

to

$(function () {
    $('#myTab li:eq(1)').click(function() {
        document.getElementById("button1").click();
    });
});

But there is another problem, when I press "GoToButton3Tab2" in tab1, the website pass through empty tab2, and then goes to button3 of tab2. 但是还有另一个问题,当我在tab1中按“ GoToButton3Tab2”时,网站会通过空白的tab2,然后转到tab2的button3。

so, I solve it by changing the order of .tab('show') and .click() 因此,我通过更改.tab('show')和.click()的顺序来解决此问题

<a href="javascript: 
    $('#myTab li:eq(1) a').tab('show');
    $('#button3').click();
    $('html,body').scrollTop(0);">GoToButton3Tab2
</a>

to

<a href="javascript: 
    $('#button3').click();
    $('#myTab li:eq(1) a').tab('show');
    $('html,body').scrollTop(0);">GoToButton3Tab2
</a>

Now, when I press "GoToButton3Tab2" in tab1,the website goes to button3 of tab2 directly. 现在,当我在tab1中按“ GoToButton3Tab2”时,网站将直接转到tab2的button3。

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

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