Building off of Alnitak's answer. Here's the working current implementation with matchesSelector
which is now matches
in the DOM spec.
// get nearest parent element matching selector
function closest(el, selector) {
var matchesSelector = el.matches || el.webkitMatchesSelector || el.mozMatchesSelector || el.msMatchesSelector;
while (el) {
if (matchesSelector.call(el, selector)) {
break;
}
el = el.parentElement;
}
return el;
}
Browser support is great: http://caniuse.com/matchesselector
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…