This just isn't possible.
At first glance, you'd have thought you'd have been able to patch your own classList
implementation into HTMLElement
if it didn't exist already;
if (!("classList" in document.createElement("div"))) {
HTMLElement.prototype.classList = {
contains: function () {
}
};
}
However:
- Your
contains
implementation has no way to access the DOMElement (this
is theclassList
object, not the element - Extending DOM objects in older IE just doesn't work.
Your only option is to replace all your usage of classList
with your own wrapper function, which uses the classList
functionality if available, and falls back to your own implementation if it doesn't.
var classListWrapper = (function () {
if ("classList" in document.createElement("div")) {
return {
add: function (el, class) {
el.className.add(class);
},
remove: function (el, class) {
el.className.remove(class);
},
// etc.
}
} else {
return {
add: function (el, class) {
el.className += ' ' + class;
},
remove: function (el, class) {
el.className = (' ' + el.className + ' ').replace(' ' + class + ' ', ' ');
},
// etc.
};
}
}());
... and then in your code use classListWrapper.add(element, class)
instead.