我的网页中有 3 个部分,它们在单击时变得可见或不可见(这是使用 CSS 完成的)。
然后我可以使用 anchor 从导航栏链接这 3 个部分(在页面上, anchor 标记具有“togg”类),当我单击一个链接时,它会转到正确的部分,但我需要相关的部分也变得可见。
我在本网站和其他网站上搜索了可能的答案,并确定了我认为可行的答案:
我目前在 body onload 中调用了一个名为“hasher”的 javascript 函数。
函数然后说
function hasher()
{
if(!window.location.hash)
{
return;
}
else
{
var hasher = unescape(window.location.hash.substring(1));
$('a.togg[href=#"' + hasher + '"]').click();
}
但这似乎行不通,而且我对 Javascript 的了解不足以让我弄清楚从这里到哪里去。
请有人帮忙!预先感谢您的帮助。
最佳答案
如果hasher
是需要可见使用的section元素的id
html
<div class="section" id="something"></div>
<div class="section" id="something1"></div>
<div class="section" id="something2"></div>
js
$('a.togg[href=#"' + hasher + '"]').click();
$(".section").not("#"+hasher").hide(); //Hides all sections except the one that has id in hasher
$('#'+hasher).show(); //shows the section with id in hasher
关于javascript - CSS 从 url 切换 anchor ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18918145/