HTML DOM Element classList
❮ Obiekt elementuPrzykład
Dodaj klasę "myStyle" do elementu:
element.classList.add("myStyle");
Usuń klasę „myStyle” z elementu:
element.classList.remove("myStyle");
Przełącz między dwiema klasami dla elementu:
element.classList.toggle("newStyle");
Więcej przykładów poniżej.
Definicja i użycie
Właściwość classList
zwraca nazwy klas CSS elementu.
Właściwość classList
zwraca obiekt DOMTokenList.
Właściwość classList
jest tylko do odczytu, ale możesz użyć metod add() i remove() do dodawania lub usuwania klas CSS.
Notatka
Właściwość classList
jest przydatna zarówno do dodawania, usuwania i przełączania klas CSS na elemencie.
Zobacz też:
Składnia
element.classList
Wartości nieruchomości
Value | Description |
length | The number of classes in the list. This property is read-only. |
Metody
Method | Description |
add(class1, class2, ...) |
Adds one or more classes. |
contains(class) |
Returns true if an element has the class, otherwise false .
|
item(index) | Returns the class name with a specified index. Index starts at 0. Returns null if the index is out of range. |
remove(class1, class2, ...) | Removes one or more classes from an element. Removing a class that does not exist, does NOT throw an error. |
toggle(class, true|false) | Toggles between a class for an element. It removes the class and returns false .If the class does not exist, it adds the class and returns true .The optional second parameter is a boolean that forces the class to be added or removed, regardless of whether or not it already exists. The second parameter is not supported in Internet Explorer 11 or earlier. |
Wartość zwrotu
Type | Description |
Object | A DOMTokenList. A list of the class names of an element. |
Więcej przykładów
Dodaj wiele klas do elementu:
element.classList.add("myStyle", "anotherClass", "thirdClass");
Usuń wiele klas z elementu:
element.classList.remove("myStyle", "anotherClass", "thirdClass");
Ile nazw klas ma element:
let numb = element.classList.length;
Pobierz nazwy klas elementu „myDIV”:
<div id="myDIV" class="myStyle anotherClass thirdClass">
<p>I am myDIV.</p>
</div>
const list = document.getElementById("myDIV").classList;
Zdobądź pierwszą klasę elementu:
let className = element.classList.item(0);
Czy element ma klasę „myStyle”?
let x = element.classList.contains("myStyle");
Usuń „anotherClass”, jeśli element ma klasę „myStyle”.
if (element.classList.contains("mystyle")) {
element.classList.remove("anotherClass");
}
Przełączaj się między klasami, aby utworzyć przycisk rozwijany:
document.getElementById("myBtn").onclick = function() {myFunction()};
function myFunction() {
document.getElementById("myDropdown").classList.toggle("show");
}
Utwórz lepki pasek nawigacyjny:
// Get the navbar
var navbar = document.getElementById("navbar");
//
Get the offset position of the navbar
var sticky = navbar.offsetTop;
// Add the sticky class to the navbar when you reach its scroll position. Remove the sticky class when you leave the scroll position.
function myFunction() {
if (window.pageYOffset
>= sticky) {
navbar.classList.add("sticky")
}
else {
navbar.classList.remove("sticky");
}
}
Obsługa przeglądarki
element.classList
jest obsługiwany we wszystkich przeglądarkach:
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | 10-11 | Yes | Yes | Yes | Yes |
❮ Obiekt elementu