我想使用jQuery制作一个褪色导航菜单,其中与当前页面对应的“按下”按钮与“未按下”按钮的行为不同(具体来说,它不会在悬停时褪色为不同的颜色)。如果我查看 www.guitaracademy.nl 上的示例,我发现他们使用带有 window.location.hash 属性的本机 javascript。
但是,我似乎无法将此哈希值放入 jQuery 中。这是一个示例脚本:
<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
var p=window.location.hash;
$("#clickme").click(function(){
alert(p)
});
});
</script>
</head>
<body>
<a href="#test">Click me first</a>
<div id="clickme">Then click me</div>
</body>
</html>
加载此页面后,单击“先单击我”链接;然后在地址栏中我看到附加到原始 URL 的“#test”。但是,如果我然后单击“然后单击我”div,我会看到一个空警报。似乎哈希没有“更新”。
我将不胜感激任何帮助。