繁体   English   中英

如何根据URL中的锚标记#id激活类

[英]How to activage class based on anchor tag #id in URL

不久前,我注意到一个页面,该页面通过从URL获取#some_random_id突出显示了页面的某些区域。

例如/mypage-destination/#codex_destination_5显然会将您带到相关页面区域,但是请突出显示该区域,以免您错过它。

我调查了一下,但似乎找不到提取URL属性#锚定目标的方法。

document.getElementById(window.location.hash.substring(1)).style.backgroundColor = "#aaa";

您可以使用window.location.hash来获取URL中链接的ID(使用您的示例将返回#codex_destination_5)。 然后,您可以使用substring(1)省略哈希符号(#codex_destination_5变为codex_destination_5)。

将其用作ID将是下一个逻辑步骤。

暂无
暂无

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

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