Javascript-how to correctly highlight the navigation link of the current page?

Source: Internet
Author: User
This tab is highlighted when I click this link for the website navigation like above. when I click another page, it will jump to the corresponding page, the navigation labels corresponding to the current page will also be highlighted. what is the general design scheme? What are some good and elegant solutions? Please...



This tab is highlighted when I click this link for the website navigation like above. when I click another page, it will jump to the corresponding page, the navigation labels corresponding to the current page will also be highlighted. what is the general design scheme? What are some good and elegant solutions? Please answer.
My personal solutions are:
1. traverse the URLs of these tags during windows loading. if they are the same as the url of the current page, they will be highlighted;
2. use cookies to store the navigation index of the current click, read the index value after entering the current page, and highlight the tag marked by the index value;

I feel that these two methods are a bit unreliable, so I want a better idea.

Reply content:



This tab is highlighted when I click this link for the website navigation like above. when I click another page, it will jump to the corresponding page, the navigation labels corresponding to the current page will also be highlighted. what is the general design scheme? What are some good and elegant solutions? Please answer.
My personal solutions are:
1. traverse the URLs of these tags during windows loading. if they are the same as the url of the current page, they will be highlighted;
2. use cookies to store the navigation index of the current click, read the index value after entering the current page, and highlight the tag marked by the index value;

I feel that these two methods are a bit unreliable, so I want a better idea.

JavaScript

You can usehrefThe property is the same as the page URL.

You can also add a custom data attribute for each element, as shown in figuredata-patternThen put the regular match in it.
JSFiddle: https://jsfiddle.net/bd4g5f2h/

I don't think this is necessary. There are three or five webpages. You can create a highlighted class. Then you can set a highlighted navigation for each page. If you use a template engine. In this step, you can use the template engine. If you use a single-page application, consider using js, or judging # The Following.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.