首页 > 解决方案 > 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);
  })();
  
});

非常感谢任何帮助

干杯

标签: javascriptjquerywordpress

解决方案


推荐阅读