在url中使用#打开html div元素

时间:2017-11-09 14:09:33

标签: javascript jquery html css url

我有一个包含两个标签的页面(主页|操作)。

当我悬停“操作”标签时,网址会显示为#tab_action:

enter image description here

然后当我点击时,它会打开相关的标签内容:

enter image description here

当我手动输入网址时:

本地主机:8088 / HSE /公共/观测/ 3 /节目#tab_action

打开主页选项卡而不是操作选项卡。

如何使上述URL打开“操作”选项卡,与单击“操作”选项卡时相同。

3 个答案:

答案 0 :(得分:0)

onload / document ready读取哈希值,如果有则使用它。

$(function(){
  var hash = window.location.hash;
  if (hash && hash.length>1) {
    $(hash).click()
  }
})

答案 1 :(得分:0)

在页面加载时检查window.location.hash(网址哈希)并在js代码中切换标签。

答案 2 :(得分:0)

您正在寻找WindowEventHandlers.onhashchange。Here您可以找到文档

if ("onhashchange" in window) {
    alert("The browser supports the hashchange event!");
}

function locationHashChanged() {
    if (location.hash === "#tab_action") {
        somecoolfeature();
    }
}

window.onhashchange = locationHashChanged;