我有以下代码段:
myPanel.getElement().setId("left-content");
//...
//...
Element e = DOM.getElementById("left-content");// this returns NULL!
以下是较长的代码段:
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 !!!
// ....
}
上述复合词正被添加到RootPanel
。
答案 0 :(得分:10)
仅当请求的窗口小部件实际附加到DOM时,才能使用DOM.getElementById
。
您的代码显示您在构造函数中设置id
并在尚未附加窗口小部件后立即请求对应元素。只有在调用onLoad方法时才会附加您的窗口小部件。
答案 1 :(得分:6)
我假设您在调用RootPanel.get().add(myPanel)
之前没有将myPanel添加到DOM(例如DOM.getElementById("left-content")
)。