Clearing off :target CSS psuedo class

不想你离开。 提交于 2019-11-29 17:08:24

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/

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>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!