实际上我真的不知道如何表述问题标题,但这里是描述。假设我使用 jQuery 来显示/隐藏彼此堆叠(绝对定位)的 ul
。例如:
<ul id="one">
<li>blah</li>
<li>blah2</li>
</ul>
<ul id="two">
<li>blah</li>
<li>blah2</li>
</ul>
我有一个 Controller 按钮,按下时,只需更改这些 ul
的 z-index。 Controller 按钮实际上只是:
<a href="#" id="mybutton">My button</a>
使用 jQuery 代码:(我正在使用 jQuery 循环插件)
$('#mybutton').click(function() {
// check which ul is currently shown
// change z-index, so that the next ul is to be shown
});
问题:
在我的站点中,我有几个页面想指向 second ul
,以便在单击时将它们带到页面所有 ul
,但只有 second 会显示。如果此人转到该页面,显示默认的第一个 ul
,然后单击“下一步”继续下一个 ul
,这将是相同的。我只是想知道是否可以避免按“下一步”,而只是将用户直接带到页面并显示第二个 ul
。
最佳答案
我认为您可以使用 URL 中的井号标签。然后你可以像这样写一个 if 语句:
if(location.hash === "#2"){
$("#one").hide();
}else{
$("#two").hide();
}
或者直接作为复制粘贴的例子:
<html>
<script src="http:////ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
<script>
$(function(){
if(location.hash === "#2"){
$("#one").hide();
}else{
$("#two").hide();
}
$('#mybutton').click(function(e) {
$("ul").toggle(); //quick n dirty! only works with 2 lists :)
e.preventDefault();
});
});
</script>
<body>
<a href="#" id="mybutton">My button</a>
<ul id="one">
<li>First Item!</li>
<li>blah</li>
<li>blah2</li>
</ul>
<ul id="two">
<li>Second Item!</li>
<li>blah</li>
<li>blah2</li>
</ul>
<a href="#2">To second page (you might have to refresh, notice the #2 at the end of the url!)</a>
</body>
</html>
另请注意,我在 #mybutton
的点击监听器中插入了一个 e.preventDefault();
以防止 URL 在点击时变回。
关于javascript - 使用 jquery 创建导航页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12438000/