Skip to content

Instantly share code, notes, and snippets.

@deguchi deguchi/closest
Created May 11, 2018

Embed
What would you like to do?
function closest(node, selector) {
// IEや一部ブラウザでは.closet()が実装されていないので自前も用意
return (node.closest || function (_selector) {
do {
// nodeとselectorがマッチしたら返す
if ((node.matches || node.msMatchesSelector).call(node, _selector)) {
return node;
}
// マッチしなかったら親要素を代入
node = node.parentElement || node.parentNode;
} while (node !== null && node.nodeType === 1);
return null;
}).call(node, selector);
}
document.body.addEventListener('click', (e) => {
if (!closest(e.target, '#ui')) {
this.setState({ hideResult: true });
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.