2

Is there an efficient way to check if a given element matches a selector like the is()-method in jQuery (http://api.jquery.com/is/)?

I have found this solution, but I think it has bad performance:

public static String cleanSelector(String selector) {
    return selector.replaceAll(":link|:active|:visited|:hover|:after|:focus", "");
}

public static boolean elementIs(Element elem, String selector, Element root) {
    Elements elems = root.select(cleanSelector(selector));
    return elems.contains(elem);
}
4

1 回答 1

0

使用包私有QueryParser类:

Evaluator eval = QueryParser.parse(query);
boolean result = eval.matches(element.ownerDocument(), element);
于 2014-05-24T16:19:29.233 回答