本文介绍了JS相当于jQuery .is()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有纯粹的JS等价于jQuery .is()(仅限现代浏览器)。

Is there a pure JS equivalent of jQuery .is() (modern browsers only).

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

I know there is querySelector, but I want to check the node itself, rather than finding children nodes.

推荐答案

看起来 matchesSelector 就是我想要的。

Polyfill在这里:

Polyfill is here:

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);

这篇关于JS相当于jQuery .is()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 05:00