How do I highlight a link based on the current page?

后端 未结 7 514
日久生厌
日久生厌 2020-12-30 08:21

Sorry if this sounds like a really stupid question, but I need to make a link change colour when you are on the page it links to.

For example, when you are on the \"

相关标签:
7条回答
  • 2020-12-30 08:58

    You need code on the server for this. A simplistic approach is to compare the URL of the current page to the URL in the link; however consider that there are many different URLs in stackoverflow which all result in the 'Questions' tab being highlighted.

    A more sophisticated version can either put something in the session when you change pages (not too robust); store a list of pages/URL patterns which are relevant to each menu item; or within the code of the page itself, set a variable to determine which item to highlight.

    Then, as John Millikin suggests, put a class on the link or on one of its parent elements such as "current-page" which will control the colour of it.

    0 讨论(0)
提交回复
热议问题