是否有 jQuery 的纯 JS 等价物.is()
在现代浏览器上?
我知道有querySelector
方法,但我想检查节点本身,而不是查找子节点。
好像matchesSelector
就是我想要的。
https://developer.mozilla.org/en-US/docs/Web/API/Element.matches https://developer.mozilla.org/en-US/docs/Web/API/Element.matches
Polyfill 在这里:
https://gist.github.com/jonathantneal/3062955 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);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)