• і
  • ...">

    JS: indicating the current item in the menu

    I have a menu:

     <ul>
        <li id="active"><a href="/"></a></li>
         <li><a href="/about/"> і</a></li>
         <li><a href="/work/">іі</a></li>
         <li><a href="/initiative/">Ііі</a></li>
         <li><a href="/statti/">і</a></li>
     </ul>
    

    Can you suggest a way to change id = the active attribute for this list. If, for example, I clicked, I want this element li to be marked as active.

    thanks

    +3
    source share
    4 answers

    There is an elegant css method to create an active navigation state without javascript or server support. In principle, a single id (for example, as an attribute of the body id) that uniquely identifies the navigation state is enough to create css selectors that highlight the active state.

    +2
    source

    , . , , , , "", , , "actiuve" . "" .

    +2

    , , "xpto", onclick ​​:

    onclick="setActiveId(this);"
    
    
    functionSetActiveId(newActiveElem){
      var allElems = document.getElementById('xpto').childNodes;
    
      for(var i = 0; i < allElems.length; i++){
        allElems[i].id = '';
      }
    
      newActiveElem.id = 'active';
    }
    
    +1

    Source: https://habr.com/ru/post/1711860/


    All Articles