提问者:小点点

当来自其他站点时,如何自动打开标签页?


目前,我有一个网站的元素表。 这些元素有一些子元素。 我有第二个站点,在那里我可以看到元素和子元素的许多选项卡。 即时消息现在给网站打电话的方式是:

<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>

null

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


共1个答案

匿名用户

在第一个站点中,您必须设置如下链接

<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();
}