javascript - Link to open tab from menu help. jQuery Tabslet
问题描述
I'm wanting to link to a certain tab (Portfolio Tab) on a page from the main menu of a website, so when clicked it goes to that page with that portfolio tab open.
So far I've come up with this (using jQuery Tabslet) which works when not on the same page, but doesn't work if the user happens to be on the same page as the tabs, and so does nothing.
The link I use in the main menu is /about/#tab-3 which is doing the job of going to the about page with the portfolio tab open.
I thought I may need to trigger a page refresh when on the same page? And perhaps remove the #tab-3 from the url too.
Not being a jQuery expert, I unfortunately just don't know.
Here is the code so far
Thanks in advance.
jQuery(document).ready(function($){
$('.tabs').tabslet({
active :1,
animation : true,
container: '.tabs-container'
});
var hash = $.trim( window.location.hash );
var anchor = $('a[href$="'+hash+'"]');
if (anchor.length > 0){
anchor.click();
}
window.onload = function () {
if (location.hash) {
window.scrollTo(0, 0);
}
};
});
解决方案
推荐阅读
- javascript - 删除 setInterval
- c# - 检查多扩展列表框中的选定索引
- php - laravel .env 电子邮件密码不接受,抛出错误
- docker - 如何在 Azure DevOps 中标记 Docker 映像?
- arrays - 如何使用php的strpos函数从数组中统计相似的单词?
- json - 使用 Powershell 将文件的内容转换为可以使用 JSON 传输的字符串
- reactjs - Why does my NavLink's doesn't set to active using Semantic-ui-react?
- javascript - 如何使用 vuex 删除项目?
- debugging - 如何强制调试跟踪语句按顺序评估?
- gradle - 如何通过 LibGDX (Gradle) 导入和使用 JsInterop?