我正在编写一个涉及与 iframe 中的外部文档交互的 GWT 应用程序。作为概念证明,我正在尝试将点击处理程序附加到按钮。
以下在 javascript 中有效
var iframe = document.getElementById("rawJSIFrame");
var doc = iframe.contentDocument;
var body = doc.body;
var button = doc.getElementsByTagName("input").namedItem("submit");
button.onclick = function() {
alert("Clicked!");
};
尝试在 GWT 中做同样的事情,我做了以下事情:
public void addClickHandlerToSubmitButton(String buttonElementName, ClickHandler clickHandler) {
IFrameElement iframe = IFrameElement.as(frame.getElement());
Document frameDocument = getIFrameDocument(iframe);
if (frameDocument != null) {
Element buttonElement = finder(frameDocument).tag("input").name(buttonElementName).findOne();
ElementWrapper wrapper = new ElementWrapper(buttonElement);
HandlerRegistration handlerRegistration = wrapper.addClickHandler(clickHandler);
}
}
private native Document getIFrameDocument(IFrameElement iframe)/*-{
return iframe.contentDocument;
}-*/;
下面是ElementWrapper类:
public class ElementWrapper extends Widget implements HasClickHandlers {
public ElementWrapper(Element theElement) {
setElement(theElement);
}
public HandlerRegistration addClickHandler(ClickHandler handler) {
return addDomHandler(handler, ClickEvent.getType());
}
}
查找按钮的代码工作正常,但未调用实际的单击事件处理程序。以前有没有人遇到过类似的问题,你是如何解决的?
提前致谢
锡
最佳答案
Hilbrand 对 GWT 方法 onAttach()
未被调用的问题的看法是正确的。
我实现了您最初的解决方案,将以下方法添加到 ElementWrapper
:
public void onAttach() {
super.onAttach();
}
并在创建 ElementWrapper
后调用添加的 wrapper.onAttach()
。就像一个魅力!
关于java - 将 GWT 事件挂接到外部 iframe 中的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1140796/