簡體   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