如何在GWT中访问HTML对象中的DOM

时间:2013-10-06 12:10:10

标签: gwt

如何在通过RequestBuilder获取的HTML对象中访问DOM?以下是关于我的问题的缩短代码:

RequestBuilder requestBuilder = new RequestBuilder(
    RequestBuilder.POST, 
    "/experiment.txt");

requestBuilder.setCallback(new RequestCallback() {
    public void onResponseReceived(Request request, Response response) {
        if (200 == response.getStatusCode()) {
            HTML responseHTML = new HTML(response.getText());
            //TODO parse responseHTML
        } 
    }

    public void onError(Request request, Throwable exception) {}
});

假设以下是responseHTML

的内容
HTML responseHTML=new HTML(
    "<div id='div_1'>my div 1</div>" + 
    "<div id='div_2'>my div 2</div>");

是否可以通过以下方式从div_1内获取responseHTML

responseHTML.getElementById("div_1");

2 个答案:

答案 0 :(得分:1)

responseHTML附加到DOM后,您可以使用Dom.getElementById(String id)

如果您尚未将responseHTML附加到DOM,则可以尝试使用GQuery framework来选择/查找所需的DOM元素。简单的例子是:

// select div with id=div_1 from responseLabel element
GQuery divWithIdQuery = GQuery.$("div#div_1", responseLabel);

// assuming that only one div was selected you can get its content :
String div1HTML = divWithIdQuery.toString();

// You can get the Element as well
Element div1Element = divWithIdQuery.get(0);

答案 1 :(得分:0)

是的,你可以

import com.google.gwt.user.client.DOM;


    Element elementById = DOM.getElementById("div_1");

现在使用elementById

确保在您到达之前我已经添加到DOM中。