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

后端 未结 7 573
日久生厌
日久生厌 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:53

    Server side code is the easiest, by just setting a class on the link on the current page, but this is also possible on the client-side with JavaScript, setting a second class on all elements in a particular class which have an href which matches the current page.

    You could use either document.getElementsByTagName() or document.links[] and look only for those in a class denoting your navigation links and then set a second class denoting current if it matches the current URL.

    The URLs will be relative, while document.URL will not. But you can sometimes have this same problem with relative vs. absolute on the server-side if you are generating content from a table-driven design and the users can put either absolute or relative URLs anyway.

提交回复
热议问题