Clearing off :target CSS psuedo class

前端 未结 2 885
陌清茗
陌清茗 2020-12-21 21:25

Lately I\'ve been working on Shower HTML5+CSS3 presentation template to make a upcoming presentation. Shower uses the CSS :target pseudo-class which makes it easy to identif

相关标签:
2条回答
  • 2020-12-21 21:35

    To clear the hash target with a link without using Javascript, you can just make another <a> tag with href="#".

    Simple example: http://jsfiddle.net/Y5XB7/
    Click on the top black square for the :target to be active, then click on the bottom black square to clear it.

    <a href="#" ...> ... </a>
    
    0 讨论(0)
  • 2020-12-21 21:41

    I previously tried the following two statements and it didn't work (at least on Firefox):

    history.pushState({}, 'null', window.location.pathname);
    window.location.href.substr(0, window.location.href.indexOf('#'));
    

    The trick is to use this instead:

    window.location.hash = '';
    

    The problem only exists if we're trying to clear the :target selection through JavaScript. Anchor tags with non-existing or empty hashes works: http://jsfiddle.net/SMbsb/3/

    0 讨论(0)
提交回复
热议问题