var ul = document.querySelector("ul"); var li = ul.querySelectorAll("li"); var t; ul.onclick = function(e) { var target = e.target; if (target.tagName = "LI") { home(target); return; }; }; function home(node) { t = node; t.classList("oli") // не добавляется класс при нажатии } 
 .oli { background-color: red; } 
 <ul> <li>Первый</li> <li>Второй</li> <li>Третий</li> <li>Четвертый</li> <li>Пятый</li> </ul> 

    3 answers 3

    Incorrect use of classList It will be classList.add('class');

      How can I find in the help

      classList - This is a collection, not a function. Therefore, when you try to use it as a function, you get the corresponding exception: classList is not a function

      There is also a help in the list of methods present in this collection.

      To add, use the add method.

       var ul = document.querySelector("ul"); var li = ul.querySelectorAll("li"); var t; ul.onclick = function(e) { var target = e.target; if (target.tagName = "LI") { home(target); return; }; }; function home(node) { t = node; t.classList.add("oli") // не добавляется класс при нажатии } 
       .oli { background-color: red; } 
       <ul> <li>Первый</li> <li>Второй</li> <li>Третий</li> <li>Четвертый</li> <li>Пятый</li> </ul> 

        classList is not a function, but a DOMTokenList object ( see also ).

         console.info(typeof document.querySelector('div').classList); // object 
         <div></div> 

        Methods :
        add() - Adds a value to the DOMTokenList object, if not present.

        contains() - Checks whether the DOMTokenList object DOMTokenList passed value or not.

        item() - Returns the value at the specified index.

        remove() - Removes a value from the DOMTokenList object, if any.

        toggle() - If the DOMTokenList contains the value passed to the method, it will be removed, otherwise it will be added.

        They are more, but it is the most used

        Properties :
        length - The number of elements (or characters).