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

标签 javascript jquery html css url

我有一个包含两个选项卡(主页|操作)的页面。

当我将鼠标悬停在“操作”选项卡上时,URL 会显示带有#tab_action :

enter image description here

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

enter image description here

当我手动输入 URL 时:

localhost:8088/hse/public/observation/3/show#tab_action

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

如何使上面的 URL 打开操作选项卡,就像我点击操作选项卡时一样。

最佳答案

onload/document ready read the hash,如果它在那里使用它。

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

关于javascript - 在 url 中使用 # 打开 html div 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47203949/

相关文章:

php - 传递变量的更好方法?

javascript - Extjs 多柱形图

javascript - 在 Cordova/Onsen 中引用模板化 HTML 元素

javascript - 计算每行的价格

用于动态 HTML 表格生成的 Javascript 库

html - 嵌入 DIV 中的表格

javascript - 在 jquery 中的表行中执行单击或焦点操作

javascript - 嵌套页面不使用 HOC 加载静态文件

javascript - 在数组内上下移动对象

javascript - 如何将 WordPress 帖子 ID 传递到 HTML5 本地存储