在其他页面中打开特定标签

时间:2018-07-24 15:16:34

标签: javascript html web backend

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

  

例如,我有2页(index.html和goto.html)
  在我的index.html中,有3个标签(伦敦,巴黎和东京)   如果我按goto页面中的“转到伦敦”选项卡,它将把我发送到页面索引   并打开伦敦标签,如果我按转到goto页面中的tab tab paris按钮,它将   将我发送到页面索引并打开“巴黎”标签等。

这是我的index.html代码

{
  "log_2018-07-20": {
    "mappings": {
      "ERROR": {
        "properties": {
          "message": {
            "properties": {
              "trackid": {
                "type": "long"
              },
              // other fields...
            }
          }
        }
      },
      "INFO": {
        "properties": {
          "message": {
            "properties": {
              "trackid": {
                "type": "long"
              },
              // other fields...
            }
          }
        }
      },
      // you got the idea
    }
  }
}

这是我的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>
<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>

2 个答案:

答案 0 :(得分:1)

来源: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>

答案 1 :(得分:0)

只需将此部分添加到您的代码中即可。

<a href="index.html#defaultOpen" target="_blank">

应该可以