How to open a hidden div when a hash is on the URL?

后端 未结 3 1809
孤城傲影
孤城傲影 2021-01-13 19:55

I\'m using this javascript code to have a couple \"show/hide\" toggled divs on my site:



        
3条回答
  •  时光说笑
    2021-01-13 20:27

    This is not the javascript answer that you want, but you could try playing around with the :target pseudo selector. For instance,

    
    
    show this with #foo.
    #bar shows this.

    Example: http://jsfiddle.net/ZAHns/4/ (thanks to Jared for the idea of adding the anchors).

    Depending on what you are trying to do, this could possibly work, but think your requirements through.

    Note: Take this response with a HUGE grain of salt -- don't use it.


    To answer the actual question, use the following to determine if the hash is present:

    var in_hash = location.hash.slice(1) === what_you_are_looking_for;
    if (in_hash) ? /* IN HASH */ : /* NOT IN HASH */;
    

提交回复
热议问题