Giả sử bạn có một phần tử trong DOM:
Nhận tham chiếu đến phần tử DOM đó:
const el = document.querySelector("#el");
Sau đó, bạn có thể thao tác các lớp trên phần tử đó với classList
phương thức.
// Add a class el.classList.add("open"); // Add many classes el.classList.add("this", "little", "piggy"); let classes = ("is-message", "is-warning"); el.classList.add(… classes); // Remove a class el.classList.remove("open"); // Remove multiple classes el.classList.remove("this", "little", "piggy"); // Loop over each class el.classList; // DOMTokenList (pretty much an array) el.classList.forEach(className => ( // don't use "class" as that's a reserved word console.log(className); )); for (let className of $0.classList) ( console.log(className); ) el.classList.length; // integer of how many classes there are // Replace a class (replaces first with second) el.classList.replace("is-big", "is-small"); // Toggle a class (if it's there, remove it, if it's not there, add it) el.classList.toggle("open"); // Remove the class el.classList.toggle("open", false); // Add the class el.classList.toggle("open", true); // Add the class with logic el.classList.toggle("raining", weather === "raining"); // Check if element has class (returns true or false) el.classList.contains("open"); // Look at individual classes el.classList.item(0); // hot el.classList.item(1); // dog el.classList.item(2); // null el.classList(1); // dog
Hỗ trợ trình duyệt
Dữ liệu hỗ trợ trình duyệt này là từ Caniuse, có nhiều chi tiết hơn. Một con số cho biết rằng trình duyệt hỗ trợ tính năng ở phiên bản đó trở lên.
Máy tính để bàn
Trình duyệt Chrome | Firefox | I E | Cạnh | Safari |
---|---|---|---|---|
28 | 26 | 11 | 12 | 7 |
Di động / Máy tính bảng
Chrome dành cho Android | Android Firefox | Android | iOS Safari |
---|---|---|---|
88 | 85 | 4.4 | 7,0-7,1 |