Last active
January 26, 2019 01:21
-
-
Save cachaito/7791738 to your computer and use it in GitHub Desktop.
element.classList - alternatywa dla element.className (nie działa w IE < 10)
posiada metody:
- add
- remove
- toggle
- contains
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//div is an object reference to a <div> element with class="foo bar" | |
div.classList.remove('foo'); | |
div.classList.add('anotherclass'); | |
div.classList.add('foo','bar'); //add multiple classes | |
div.classList.toggle('visible'); | |
alert(div.classList.contains('foo')); | |
//stary sposób | |
div.className = div.firstClass + ' otherclass'; | |
//starsze metody dodawania klas | |
div.setAttribute('class', 'nazwaKlasy'); | |
div.className = 'nazwaKlasy' //można od razu dodać więcej klas! | |
div.className += ' nazwaKlasy2' //dodawanie kolejnej klasy | |
//zamiennik dla .classList.contains('nazwKlasy') | |
!!div.className.match(nazwaKlasy); //zwraca tablicę ze znalezionymi dopasowaniami zamienioną na wartość true/ false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment