这是一个使用 GWT Element 和 Node 类来查找具有给定类名的单个嵌套元素的示例。这不像文字 CSS 选择器那样开放和强大,但可以根据您的特定用例的需要进行修改:
static public Element findFirstChildElementByClassName( Widget w, String className ){
return findFirstChildElementByClassName( w.getElement(), className );
}
static private Element findFirstChildElementByClassName( Element e, String className ){
for( int i=0; i != e.getChildCount(); ++i ){
Node childNode = e.getChild(i);
if( childNode.getNodeType() == Node.ELEMENT_NODE ){
Element childElement = (Element)childNode;
if( childElement.getClassName().equalsIgnoreCase( className ) )
return childElement;
else if( childElement.hasChildNodes() ){
Element grandChildElement =
findFirstChildElementByClassName(
childElement, className );
if( grandChildElement != null ) return grandChildElement;
}
}
}
return null;
}