يطابق HTML DOM الطريقة ()
❮ عنصر العنصرمثال
حدد ما إذا كان العنصر يطابق محدد CSS:
var element = document.getElementById("myElement");
if (element.matches(".container")) {
element.innerHTML = "This element matches the \".container\" CSS selector";
} else {
element.innerHTML = "This element does not match the \".container\" CSS selector";
}
التعريف والاستخدام
تقوم matches()
الطريقة بإرجاع قيمة منطقية تشير إلى ما إذا كان عنصر ما يطابق محدد CSS معين أم لا.
نصيحة: للحصول على قائمة بجميع محددات CSS ، انظر إلى مرجع محددات CSS الخاص بنا .
دعم المتصفح
تحدد الأرقام الواردة في الجدول إصدار المتصفح الأول الذي يدعم الطريقة بالكامل.
Method | |||||
---|---|---|---|---|---|
matches() | 33.0 | 18.0 | 34.0 | 7.0 | 21.0 |
بناء الجملة
element.matches(selectors)
قيمه المعامل
Parameter | Description |
---|---|
selectors |
Required. Specifies one or more CSS selectors to match the element. These are used to select HTML elements based on their id, classes, types, attributes, values of attributes, etc. For multiple selectors, separate each selector with a comma. The returned element depends on which element that is first found in the document (See "More Examples"). Tip: For a list of all CSS Selectors, look at our CSS Selectors Reference. |
تفاصيل تقنية
قيمة الإرجاع: |
منطقي ، يشير إلى ما إذا كان يمكن مطابقة العنصر بواسطة محدد CSS:
|
---|
مزيد من الأمثلة
مثال
اكتشف ما إذا كان العنصر يطابق أحد المحددات المتعددة:
var element = document.getElementById("myElement");
if (element.matches(".container, .wrapper")) {
element.innerHTML = "This element matches either the \".container\" CSS selector or the \".wrapper\" selector.";
} else {
element.innerHTML = "This element does not match any of the selectors.";
}
الصفحات ذات الصلة
مرجع JavaScript: طريقة element.closest () .
❮ عنصر العنصر