简体   繁体   English

有没有一种有效的方法来测试元素是否与Jsoup中的选择器匹配?

[英]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/ )? 有没有一种有效的方法来检查给定元素是否与jQuery( http://api.jquery.com/is/ )中的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: 使用包私有的QueryParser类:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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