search

Home  >  Q&A  >  body text

javascript - After clicking nav, using JS to add color, how can the changed color be maintained after the page jumps?

Each navigation page is the same piece of HTML linked through the background tag. After clicking the navigation, I change the color through js, but when I click to jump to a page, the color changed by js becomes invalid. How to solve this problem?

ringa_leeringa_lee2756 days ago1642

reply all(12)I'll reply

  • PHP中文网

    PHP中文网2017-05-16 13:23:25

    I’ll answer it myself. It doesn’t need to be as complicated as what you wrote. Just add a piece of CSS to each page

    //首页
    ul li a:nth-child(1){
                color: #FF0000;
            }
    //品牌介绍
    ul li a:nth-child(2){
                color: #FF0000;
            }
    //招牌美食
    ul li a:nth-child(3){
        .........
    }

    reply
    0
  • 世界只因有你

    世界只因有你2017-05-16 13:23:25

    Save what the heck, I entered page a from the navigation bar, closed it, and then entered page b from the address bar. Will you highlight page a for me at this time?
    Write a parsing function common to all pages, and directly use the nav address to match the href

    reply
    0
  • PHPz

    PHPz2017-05-16 13:23:25

    Tell me the idea and add js judgment to each page

    假设跳转之后页面的地址是http://********/zhaoshang.html
    
    if(/zhaoshang/.test(window.location.pathname)){
    
    招商加盟.style.color="红色"
    
    }else if(){
    ...
    }

    reply
    0
  • 巴扎黑

    巴扎黑2017-05-16 13:23:25

    A few ways:

    1. The navigation bar has its own iframe;

    2. Use sessionStorage / localStorage to save state;

    3. Jump page with URL parameters&active =xxx;

    4. Using spa does not really jump to the page;
      Looking at the answerer’s question, I guess 2 or 3 are more suitable

    reply
    0
  • 漂亮男人

    漂亮男人2017-05-16 13:23:25

    There is no need to use JS, just use the css class name. Add a class name to each nav and put it in the css file of this nav

    <nav>
        <a class="index-active">首页</a>
        <a class="details-active">详情页</a>
     </nav>
     
     在 index.css 中 写 
         .idnex-active {...}
     在 detail.css 中 写 
         .detail-active {...} 

    reply
    0
  • 世界只因有你

    世界只因有你2017-05-16 13:23:25

    Save it locally

    reply
    0
  • 我想大声告诉你

    我想大声告诉你2017-05-16 13:23:25

    The background can determine which page the current page is and then add a category. This is more convenient.

    reply
    0
  • 黄舟

    黄舟2017-05-16 13:23:25

    Save locally, and when you enter the page again, read the cache and make a judgment

    reply
    0
  • 巴扎黑

    巴扎黑2017-05-16 13:23:25

    Save status to cookie.

    Save to session, but this requires the cooperation of back-end development.

    reply
    0
  • 天蓬老师

    天蓬老师2017-05-16 13:23:25

    Add active to the li class, set the font color of active, and dynamically add classes to each li!

    reply
    0
  • Cancelreply