قائمة فئة عنصر HTML DOM
❮ عنصر الكائنمثال
أضف فئة "myStyle" إلى عنصر:
element.classList.add("myStyle");
إزالة فئة "myStyle" من عنصر:
element.classList.remove("myStyle");
التبديل بين فئتين لعنصر ما:
element.classList.toggle("newStyle");
المزيد من الأمثلة أدناه.
التعريف والاستخدام
تقوم classList
الخاصية بإرجاع أسماء فئات CSS للعنصر.
تقوم classList
الخاصية بإرجاع كائن DOMTokenList.
classList
الخاصية للقراءة فقط ، ولكن يمكنك استخدام أساليب add () وإزالة () لإضافة أو إزالة فئات CSS .
ملحوظة
هذه classList
الخاصية مفيدة في إضافة فئات CSS وإزالتها وتبديلها في عنصر ما.
أنظر أيضا:
بناء الجملة
element.classList
قيم الملكية
Value | Description |
length | The number of classes in the list. This property is read-only. |
طرق
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. |
قيمة الإرجاع
Type | Description |
Object | A DOMTokenList. A list of the class names of an element. |
مزيد من الأمثلة
أضف فئات متعددة إلى العنصر:
element.classList.add("myStyle", "anotherClass", "thirdClass");
إزالة فئات متعددة من عنصر:
element.classList.remove("myStyle", "anotherClass", "thirdClass");
كم عدد أسماء الفئات التي يمتلكها العنصر:
let numb = element.classList.length;
احصل على أسماء فئات العنصر "myDIV":
<div id="myDIV" class="myStyle anotherClass thirdClass">
<p>I am myDIV.</p>
</div>
const list = document.getElementById("myDIV").classList;
احصل على الدرجة الأولى من عنصر:
let className = element.classList.item(0);
هل يحتوي العنصر على فئة "myStyle"؟
let x = element.classList.contains("myStyle");
قم بإزالة "anotherClass" إذا كان العنصر يحتوي على فئة "myStyle".
if (element.classList.contains("mystyle")) {
element.classList.remove("anotherClass");
}
قم بالتبديل بين الفئات لإنشاء زر قائمة منسدلة:
document.getElementById("myBtn").onclick = function() {myFunction()};
function myFunction() {
document.getElementById("myDropdown").classList.toggle("show");
}
قم بإنشاء شريط تنقل ثابت:
// 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");
}
}
دعم المتصفح
element.classList
مدعوم في جميع المتصفحات:
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | 10-11 | Yes | Yes | Yes | Yes |
❮ عنصر العنصر