简体   繁体   中英

Is there an efficient way to test if an element matches a selector in Jsoup?

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

Using the package-private QueryParser class:

Evaluator eval = QueryParser.parse(query);
boolean result = eval.matches(element.ownerDocument(), element);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM