在现代浏览器中,是否有与jQuery .is()完全等效的JS?

我知道有querySelector方法,但是我想检查节点本身,而不是查找子节点。

最佳答案

看起来matchesSelector是我想要的。

https://developer.mozilla.org/en-US/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);

10-06 11:57