DOM.getElementById in GWT doesn't seem to work

发布于 2022-09-06 09:51:15 字数 1520 浏览 21 评论 0

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

送君千里 2022-09-13 09:51:15

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.

爱你不解释 2022-09-13 09:51:15

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文