DOM.getElementById in GWT doesn't seem to work

陌路散爱 提交于 2019-12-07 11:05:43

问题


I've the following code snippet:

myPanel.getElement().setId("left-content");
//...
//...
Element e = DOM.getElementById("left-content");// this returns NULL!

Update

Here is a longer code snippet:

public class RootComposite extends Composite
{
    public RootComposite(int comboSelectedIndex)
    {

        VerticalPanel verticalPanel = new VerticalPanel();
        initWidget(verticalPanel);
        VerticalPanel containerPanel = new VerticalPanel();
        containerPanel.setSpacing(1);
        verticalPanel.add(containerPanel);
        verticalPanel.setSize("100%", "100%");

        RightPanelMainComposite rightPanelMainComposite =  new RightPanelMainComposite();

        VerticalPanel rightcolVerticalPanel = new VerticalPanel();
        rightcolVerticalPanel.setStyleName("rightcol");
        VerticalPanel searchVerticalPanel = new VerticalPanel();
        searchVerticalPanel.setStyleName("search");
        rightcolVerticalPanel.add(searchVerticalPanel);
        searchVerticalPanel.add(rightPanelMainComposite);

        ContentComposite contentComposite = new ContentComposite();

        HorizontalPanel leftContentPanel = new HorizontalPanel();
        containerPanel.add(leftContentPanel);
        leftContentPanel.getElement().setAttribute("id", "left-content");

        DOM.getElementById("left-content"); // returns NULL !!!
        // ....
    }

The above Composite is being added to the RootPanel.


回答1:


You can use DOM.getElementById only if the requested widget is actually attached to the DOM. Your code shows that you set the id in the constructor and request the correspondant element right after when the widget is not attached yet. Your widget is actually attached only when its onLoad method is called.




回答2:


I assume, that you didn't add myPanel to the DOM (e.g. RootPanel.get().add(myPanel)) before calling DOM.getElementById("left-content").



来源:https://stackoverflow.com/questions/4920579/dom-getelementbyid-in-gwt-doesnt-seem-to-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!