javascript - Jquery window.location.hash 在主页上不起作用
问题描述
我正在建立一个客户网站,并添加了 Jquery 滚动选项卡。我将锚链接设置为从菜单中单击,因此您可以从菜单单击到特定选项卡。
它适用于标签页。https://alphadevsite1.co/vertical-tabs
但菜单项未从主页单击https://alphadevsite1.co
我错过了什么吗?这是代码:
https://codepen.io/genesis16/pen/rNjBORv
jQuery(document).ready(function($) {
// Code that uses jQuery's $ can follow here.
jQuery(function() {
$('.tabs nav a').on('click', function() {
show_content($(this).index());
});
show_content(0);
function show_content(index) {
// Make the content visible
$('.tabs .content.visible').removeClass('visible');
$('.tabs .content:nth-of-type(' + (index + 1) + ')').addClass('visible');
// Set the tab to selected
$('.tabs nav a.selected').removeClass('selected');
$('.tabs nav a:nth-of-type(' + (index + 1) + ')').addClass('selected');
}
if(window.location.hash) {
var hash = window.location.hash.substring(1); //Puts hash in variable, and removes the # character
$('.tabs nav a#'+hash).click();
console.log('.tabs nav a#'+hash);
}
$("#menu-main-menu li a").click(function(e){
e.preventDefault();
var locationString = $(this).attr("href");
var index = locationString.indexOf("#");
if (index !== -1){
var hash = locationString.substring(index+1)
console.log($(this));
$('.tabs nav a#'+hash).click();
console.log('.tabs nav a#'+hash);
}
});
});
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-36251023-1']);
_gaq.push(['_setDomainName', 'jqueryscript.net']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
});
非常感谢任何帮助
干杯
解决方案
推荐阅读
- tensorflow - 将不同高宽的tensorflow模型转成.pb格式
- c# - 在 UnitTest 中捕获 HeapException
- django - 如何在 Django admin 中选择特定区域内的城市?
- python - 错误:无法为使用 PEP 517 且无法直接安装的 pymssql 构建轮子
- d3.js - d3 堆栈的数据转换
- css - 为什么仅包含浮动项目时内联块容器不会折叠?
- cakephp - 调用未定义的方法 Cake\Controller\Component\RequestHandlerComponent::isMobile()
- spring-integration - Spring Integration (DSL):在同一个事务中运行两个 JPA 查询
- java - 如何访问在 html 内容中的 try-catch 块中声明的变量?
- java - Java 中的 Mallet 主题建模