我有一段我继承的 javascript;它被用作标签切换器。不幸的是,它不起作用。这是代码:
$(document).ready(function(){
/* This is the back button friendly tab switcher */
var trackContainers = $('.switcher > .results');
trackContainers.hide().filter(':first').show();
$(window).bind('hashchange', function () {
var hash = window.location.hash || '#dpp';
console.log('hash: ' + hash);
trackContainers.hide();
trackContainers.filter(hash).show();
$('ul.tabs li').removeClass('active');
$('a[hash='+hash+']').parent().addClass('active');
});
$(window).trigger("hashchange").location(hash);
});
应该发生的是,当单击特定选项卡时,它会更改围绕单击的选项卡的 li 标记的类。选项卡代码如下所示:
<div class="switcher">
<ul class="tabs">
<li class="inactive"><a href="#dpp">Digital Path to Purchase</a></li>
<li class="inactive"><a href="#cre">Fueling Creativity</a></li>
<li class="inactive"><a href="#bpp">Best Practices/Big Picture</a></li>
<li class="inactive"><a href="#si">Shopper Insights 101</a></li>
<li class="inactive"><a href="#dem">Who Is Your Shopper</a></li>
<li class="inactive"><a href="#gt">Google Theater</a></li>
<li class="inactive"><a href="#res">Understanding the Shopper</a></li>
<li class="inactive"><a href="#bar">Brand Activation at Retail</a></li>
<li class="active"><a href="#duc">Deeper Understanding of Center Store</a></li>
</ul>
</div>
</div>
您可以看到名为 #duc 的链接在其 li 项上有活动类。但是,当我查看 Firebug 中的脚本代码时,它给了我一个错误,说哈希未定义:
再次,查看 Firebug,但这次在控制台选项卡上,它非常清楚地显示哈希已定义:
谁能指出它是如何在 console.log 和 .trigger 行之间失去定义的?