最近,我一直在做Shower HTML5+CSS3 presentation template的演讲。 Shower使用CSS :target pseudo-class,可轻松在幻灯片概览中识别当前幻灯片。

我了解:target如何处理URL中的哈希,但是我无法从所有元素中清除:target CSS。

所以我的问题是:给定元素:target之后,我们如何清除所有:target并使用JavaScript将其还原为标准格式?使用History API似乎无效。

最佳答案

我以前尝试过以下两个语句,但它不起作用(至少在Firefox上):

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


诀窍是改用此方法:

window.location.hash = '';


仅当我们尝试通过JavaScript清除:target选择时,问题才存在。具有不存在的散列或为空的散列的锚标记可以工作:http://jsfiddle.net/SMbsb/3/

07-24 09:43
查看更多