![](/img/trans.png)
[英]How to add an active class to an anchor tag based on URL and Vanity URLs?
[英]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.