GWT中的DOM.getElementById似乎不起作用 [英] DOM.getElementById in GWT doesn't seem to work

查看:66
本文介绍了GWT中的DOM.getElementById似乎不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码片段:

  myPanel.getElement()。setId(left-content); 
// ...
// ...
元素e = DOM.getElementById(left-content); //返回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();

Horizo​​ntalPanel leftContentPanel = new Horizo​​ntalPanel();
containerPanel.add(leftContentPanel);
leftContentPanel.getElement()。setAttribute(id,left-content);

DOM.getElementById(left-content); //返回NULL!
// ....
}

上面的Composite正在添加到 RootPanel

解决方案

您可以使用 DOM.getElementById 只有当请求的小部件实际连接到DOM时。
您的代码显示您在构造函数中设置 id ,并在尚未附加窗口小部件后立即请求相关元素。您的小部件实际上仅在其 onLoad 方法。


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.

解决方案

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.

这篇关于GWT中的DOM.getElementById似乎不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆