简体   繁体   English

使用Div Anchor Link隐藏/显示div

[英]Using Div Anchor Link to hide/show divs

I have a page with 4 hidden divs. 我有一个包含4个隐藏div的页面。 If I want to link to them, I do this for example 如果我想链接到他们,例如

<a href="#about" class="nav_text" id="about_link">About</a>

This shows the about div and hides the others due to my Javascript/jQuery that I wrote for it. 由于我为它编写的Javascript / jQuery,它显示了about div并隐藏了其他div。

However, my problem arises in that the user's address bar reads as /index.php#about_content. 但是,我的问题出在用户的地址栏读为/index.php#about_content。 If you were to manually type that in, it doesn't show the about_content div because it's hidden. 如果要手动输入,则不会显示about_content div,因为它已隐藏。 How can I make it show that div if it's manually typed in? 如果手动输入div,该如何显示呢?

Read the current hash and show it on document ready. 读取当前的哈希值,并在准备好文档中显示出来。

$(document).ready(function() {
    $(window.location.hash).show();
});

You'll want to listen for a hashchange event on the window. 您需要在窗口上监听hashchange事件 Once you do that, load in the element from the hash, and, if it matches, show the proper <div> . 完成后,从哈希中加载元素,如果匹配,则显示正确的<div> You'll also want to check on initial load of the page if there is something in the hash and do the same. 您还需要检查页面的初始加载,看看哈希中是否包含某些内容,然后执行相同的操作。

Not that this is IE8+ compatible only, as IE7 and below do not support the hashchange event. 并不是说这仅与IE8 +兼容,因为IE7及以下版本不支持hashchange事件。 If you want to support them, you'll need to use one of the many tricks (or plugins ) for it. 如果要支持它们,则需要使用许多技巧之一(或插件 )。

PS You can find out what is in the hash using location.hash PS您可以使用location.hash找出哈希中的内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM