2019-08-05 11:11:08 +08:00
|
|
|
|
/**
|
|
|
|
|
* @file 担心会有些浏览器不支持,所以网上抄了一段
|
|
|
|
|
* @author mdn
|
|
|
|
|
*/
|
|
|
|
|
if (!Element.prototype.matches) {
|
2019-11-07 10:41:14 +08:00
|
|
|
|
Element.prototype.matches =
|
|
|
|
|
Element.prototype.msMatchesSelector ||
|
|
|
|
|
Element.prototype.webkitMatchesSelector;
|
2019-08-05 11:11:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!Element.prototype.closest) {
|
2020-12-29 14:51:47 +08:00
|
|
|
|
Element.prototype.closest = function (s) {
|
2019-11-07 10:41:14 +08:00
|
|
|
|
var el = this;
|
|
|
|
|
if (!document.documentElement.contains(el)) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2019-08-05 11:11:08 +08:00
|
|
|
|
|
2019-11-07 10:41:14 +08:00
|
|
|
|
do {
|
|
|
|
|
if (el.matches(s)) {
|
|
|
|
|
return el;
|
|
|
|
|
}
|
|
|
|
|
el = el.parentElement;
|
|
|
|
|
} while (el !== null);
|
|
|
|
|
return null;
|
|
|
|
|
};
|
2019-08-05 11:11:08 +08:00
|
|
|
|
}
|