来自其他站点时如何自动打开选项卡?

时间:2020-06-18 13:39:14

标签: javascript html

目前,我有一个带有元素表的网站。这些元素具有一些子元素。我还有第二个站点,在这里我可以看到元素和子元素的许多选项卡。 我现在以这种方式呼叫该网站:

<a class="dropdown-item" role="presentation" asp-controller="Scenario" asp-action="SimulationEvaluation">Auswerten</a>

因此,元素是模拟,子元素是模拟的结果。 第二页中的选项卡和功能如下:

<div class="tab" style=" border: none; overflow:hidden">
                <button class="tablinks" onclick="openTab(event, 'Ergebnis-1')" id="defaultOpen">Ergebnis-1</button>
                <button class="tablinks" onclick="openTab(event, 'Ergebnis-2')">Ergebnis-2</button>
                <button class="tablinks" onclick="openTab(event, 'Ergebnis-3')">Ergebnis-3</button>
            </div>

<div id="Ergebnis-1" class="tabcontent">
    ......
</div>
<div id="Ergebnis-2" class="tabcontent">
    ......
</div>
<div id="Ergebnis-3" class="tabcontent">
    ......
</div>

<script>

    document.getElementById("defaultOpen").click();

    function openTab(evt, name) {
        // Declare all variables
        var i, tabcontent, tablinks;

        // Get all elements with class="tabcontent" and hide them
        tabcontent = document.getElementsByClassName("tabcontent");
        for (i = 0; i < tabcontent.length; i++) {
            tabcontent[i].style.display = "none";
        }

        // Get all elements with class="tablinks" and remove the class "active"
        tablinks = document.getElementsByClassName("tablinks");
        for (i = 0; i < tablinks.length; i++) {
            tablinks[i].className = tablinks[i].className.replace(" active", "");
        }

        // Show the current tab, and add an "active" class to the button that opened the tab
        document.getElementById(name).style.display = "block";
        evt.currentTarget.className += " active";
    }
</script>

现在我的问题是:当我单击第一页表中的一个子元素时,我想让匹配选项卡自动打开。因此,当我单击Simulation-1,例如Result-3时,我希望Result-3-Tab预打开。那可能吗? ^^

1 个答案:

答案 0 :(得分:0)

在第一个站点中,您必须设置这样的链接

<a href="secondsite.com?tab=Simulation-1">Simulation-1</a>
<a href="secondsite.com?tab=Result-3">Result-3</a>

在第二个网站中,您必须在document.ready中添加此javascript代码:

const urlParams = new URLSearchParams(window.location.search);
const tabName = urlParams.get('tab');

if (tabName){
 document.querySelector('[onclick*=' + tabName + ']').click();
}