如何用我的 GWT 小部件替换元素? [英] How to replace element with my GWT widget?

查看:30
本文介绍了如何用我的 GWT 小部件替换元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以用我的小部件组件替换已知的 html 元素?(强调替换"这个词,我不想将小部件放入该元素中.:)

Is it possible to replace known html element with my widget component? (Emphasis on the word 'replace', I don't want to put the widget in that element. :)

<body>
  <img />
  <div />
  <a id="tmpEl" />
  ...
  <img />
</body>

会变成

<body>
  <img />
  <div />
  <div class="gwt-panel">...</div>
  ...
  <img />
</body>

我尝试过这样的事情......

I tried something like this...

tmpEl.getParentElement().replaceChild(myPanel.getElement(), tmpEl);

...但是生成的 DOM 元素是聋的",即它们没有收到点击事件.(要完成这项工作,我可能必须调用 RootPanel.get().adopt(widget),但该方法不可访问.)

...but the resulting DOM elements were 'deaf', i.e. they did not receive click events. (To make this work I would probably have to call RootPanel.get().adopt(widget), but that method is not accessible.)

我一度认为 HTMLPanel.addAndReplaceElement 可能是答案,但那仅当您的占位符"元素是 HTMLPanel 小部件的(直接)子元素时才有效.这显然不是我的情况.:(

For a second I thought HTMLPanel.addAndReplaceElement could be the answer, but that only works when your 'placeholder' element is (direct) child of HTMLPanel widget. Which is obviously not my case. :(

请注意,我只知道该元素的 id,而不是创建它.简单地说:我需要正是问题所说的内容.

Note please that I only know id of that element, I'm not creating it. Simply put: I need exactly what the question says.

至于更高级别的 DOM 操作":如果它允许我放置小部件而不是占位符元素,我将很乐意在可能的最高级别操作 DOM.

As for 'DOM manipulation at higher level': I will happily manipulate the DOM at highest possible level if it lets me place widget instead of that placeholder element.

推荐答案

似乎在将小部件插入 DOM 后调用 widget.onAttach() 可以解决问题.

It seems that calling widget.onAttach() after inserting widget into DOM does the trick.

class MyWidget extends Composite
{
  ...

  public void attach()
  {
    /* Widget.onAttach() is protected
     */
    onAttach();

    /* mandatory for all widgets without parent widget
     */
    RootPanel.detachOnWindowClose(this);
  }
}

tmpEl.getParentElement().replaceChild(myWidget.getElement(), tmpEl);
myWidget.attach();

Google Web Toolkit group 的 André 功劳归于安德烈.

Credit goes to André at Google Web Toolkit group.

我仍然想知道为什么没有 RootPanel.addAndReplaceElement(Widget, Element),类似于 HTMLPanel.addAndReplaceElement(Widget, Element).

I still wonder though why there is no RootPanel.addAndReplaceElement(Widget, Element), similar to HTMLPanel.addAndReplaceElement(Widget, Element).

这篇关于如何用我的 GWT 小部件替换元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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