javascript - 在其他页面中打开特定选项卡

标签 javascript html web backend

我是新手。我正在尝试通过打开特定选项卡来创建指向其他页面的链接

For Example i have 2 page (index.html and goto.html)
In my index.html there is 3 tab (London, Paris, and Tokyo) if i press go to tab london button in goto page it will send me to page index and open london tab and if i press go to tab paris button in goto page it will send me to page index and open paris tab etc.

这是我的index.html代码

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<style>
body {font-family: Arial;}

/* Style the tab */
.tab {
    overflow: hidden;
    border: 1px solid #ccc;
    background-color: #f1f1f1;
}

/* Style the buttons inside the tab */
.tab button {
    background-color: inherit;
    float: left;
    border: none;
    outline: none;
    cursor: pointer;
    padding: 14px 16px;
    transition: 0.3s;
    font-size: 17px;
}

/* Change background color of buttons on hover */
.tab button:hover {
    background-color: #ddd;
}

/* Create an active/current tablink class */
.tab button.active {
    background-color: #ccc;
}

/* Style the tab content */
.tabcontent {
    display: none;
    padding: 6px 12px;
    border: 1px solid #ccc;
    border-top: none;
}
</style>
</head>
<body>
<div class="tab">
  <button class="tablinks" onclick="openCity(event, 'London')" id="defaultOpen">London</button>
  <button class="tablinks" onclick="openCity(event, 'Paris')" id="bb">Paris</button>
  <button class="tablinks" onclick="openCity(event, 'Tokyo')" id="cc">Tokyo</button>
</div>

<div id="London" class="tabcontent">
  <h3>London</h3>
  <p>London is the capital city of England.</p>
</div>

<div id="Paris" class="tabcontent">
  <h3>Paris</h3>
  <p>Paris is the capital of France.</p>
</div>

<div id="Tokyo" class="tabcontent">
  <h3>Tokyo</h3>
  <p>Tokyo is the capital of Japan.</p>
</div>

<script>
function openCity(evt, cityName) {
    var i, tabcontent, tablinks;
    tabcontent = document.getElementsByClassName("tabcontent");
    for (i = 0; i < tabcontent.length; i++) {
        tabcontent[i].style.display = "none";
    }
    tablinks = document.getElementsByClassName("tablinks");
    for (i = 0; i < tablinks.length; i++) {
        tablinks[i].className = tablinks[i].className.replace(" active", "");
    }
    document.getElementById(cityName).style.display = "block";
    evt.currentTarget.className += " active";
}

// Get the element with id="defaultOpen" and click on it
document.getElementById("defaultOpen").click();
</script>

</body>
</html>

这是我的 goto.html 代码

    <!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>

<p>In this example, we use JavaScript to "click" on the London button, to open the tab on page load.</p>

<div class="tab">
  <a href="index.html#defaultOpen"> <button type="button" name="button">go to tab London</button> </a>
  <a href="index.html#bb"> <button type="button" name="button">go to tab Paris</button> </a>
  <a href="index.html#cc"> <button type="button" name="button">go to tab Tokyo</button> </a>
</div>

</body>
</html>

最佳答案

来源:https://www.w3schools.com/howto/howto_js_tabs.asp

默认显示选项卡

要在页面加载时打开特定选项卡,请使用 JavaScript“单击”指定的选项卡按钮:

<button class="tablinks" onclick="openCity(event, 'London')" id="defaultOpen">London</button> 
<button class="tablinks" onclick="openCity(event, 'Paris')" id="defaultOpen">Paris</button> 
<button class="tablinks" onclick="openCity(event, 'Tokyo')" id="defaultOpen">Tokyo</button>
<script>
// Get the element with id="defaultOpen" and click on it
document.getElementById("defaultOpen").click();
</script>

关于javascript - 在其他页面中打开特定选项卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51502222/

相关文章:

javascript - ajax php 中单选按钮 undefined index 值

javascript - D3 顺序尺度和线性尺度之间的区别

google-cloud-platform - 如何在 Google Cloud Storage 上托管的静态站点中强制使用尾部斜线?

带有切换功能的 jquery 帮助

python - 为网络和桌面创建应用程序

javascript - 不读取 javascript 文件

javascript - Cordova Inappbrowser - window.history.back 和forward 在executescript 函数中不起作用

javascript - 页 footer 分增加了比平时更多的高度

javascript - 如何使用 html/javascript 使欢迎对话框出现一次?

php - (.on(input, function)) 中的替代方式