Pet*_*tah 8 javascript jquery equivalent
是否有纯粹的JS等同于jQuery .is()(仅限现代浏览器).
我知道有querySelector,但我想检查节点本身,而不是找到子节点.
看起来matchesSelector就是我想要的。
https://developer.mozilla.org/zh-CN/docs/Web/API/Element.matches
Polyfill在这里:
https://gist.github.com/jonathantneal/3062955
this.Element && function(ElementPrototype) {
ElementPrototype.matchesSelector = ElementPrototype.matchesSelector ||
ElementPrototype.mozMatchesSelector ||
ElementPrototype.msMatchesSelector ||
ElementPrototype.oMatchesSelector ||
ElementPrototype.webkitMatchesSelector ||
function (selector) {
var node = this, nodes = (node.parentNode || node.document).querySelectorAll(selector), i = -1;
while (nodes[++i] && nodes[i] != node);
return !!nodes[i];
}
}(Element.prototype);
Run Code Online (Sandbox Code Playgroud)