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

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

    It really depends on how your page is constructed. Typically, I would do this using CSS, and assign give the link an id called "active"...

    <a id="active" href="thisPage.html">this page</a>
    

    ...and in the CSS...

    a#active { color: yellow; }
    

    Obviously this is a fairly simplistic example, but it illustrates the general idea.

    0 讨论(0)
  • 2020-12-30 08:39

    If for some reason you don't want to handle this on the server-side, you can try this:

    // assuming this JS function is called when page loads
    onload()
    {
      if (location.href.indexOf('/questions') > 0)
      {
        document.getElementById('questionsLink').className = 'questionsStyleOn';
      }
    }
    
    0 讨论(0)
  • 2020-12-30 08:43

    You can do this without having to actually modify the links themselves for each page.

    In the Stack Overflow clone I'm building with Django, I'm doing this:

    <!-- base.html -->
    ...
    <body class="{% block bodyclass %}{% endblock %}">
    ...
    <div id="nav">
      <ul>
        <li id="nav-questions"><a href="{% url questions %}">Questions</a></li>
        <li id="nav-tags"><a href="{% url tags %}">Tags</a></li>
        <li id="nav-users"><a href="{% url users %}">Users</a></li>
        <li id="nav-badges"><a href="{% url badges %}">Badges</a></li>
        <li id="nav-ask-question"><a href="{% url ask_question %}">Ask Question</a></li>
      </ul>
    </div>
    

    Then filling in the bodyclass like so in page templates:

    <!-- questions.html -->
    {% extends "base.html" %}
    {% block bodyclass %}questions{% endblock %}
    ...
    

    Then, with the following CSS, the appropriate link is highlighted for each page:

    body.questions #nav-questions a,
    body.tags #nav-tags a,
    body.users #nav-users a,
    body.badges #nav-badges a,
    body.ask-question #nav-ask-question a { background-color: #f90; }
    
    0 讨论(0)
  • 2020-12-30 08:53

    Set a class on the body tag for each page (manually or server-side). Then in your CSS use that class to identify which page you're on and update the style on the item accordingly.

    body.questions #questionsTab
    {
        color: #f00;
    }
    

    Here's a good longer explanation

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-30 08:54

    It's a server-side thing -- when rendering the page, add a class like "current-page" to the link. Then you can style it separately from the other links.

    For example, StackOverflow renders the links with class="youarehere" when it points to the page you're already on.

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