GWT向dom元素添加事件处理程序

时间:2010-12-12 09:44:58

标签: html dom gwt

我想将一个点击处理程序添加到gwt html面板中定义的锚点,但没有成功。我使用以下方法

panel_1 = new HTMLPanel("<div class='BackHeader'>Selected Attribute:Select an Attribute to edit(<a Id='DeleteCategory'>Delete this Attribute</a>)</div>");
  flexTable.setWidget(3, 0, panel_1);
Element DeleteCategory=panel_1.getElementById("DeleteCategory");
  Anchor C= Anchor.wrap(DeleteCategory);

但我收到以下错误:

Exception while loading module com.BiddingSystem.client.BiddingSystem. See Development Mode for details.
java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at com.google.gwt.dev.shell.ModuleSpace.onLoad(ModuleSpace.java:396) at com.google.gwt.dev.shell.OophmSessionHandler.loadModule(OophmSessionHandler.java:183) at com.google.gwt.dev.shell.BrowserChannelServer.processConnection(BrowserChannelServer.java:510) at com.google.gwt.dev.shell.BrowserChannelServer.run(BrowserChannelServer.java:352) at java.lang.Thread.run(Thread.java:662) Caused by: java.lang.AssertionError at com.google.gwt.user.client.ui.Anchor.wrap(Anchor.java:55) at com.BiddingSystem.client.View.ProductTaxonomyView.(ProductTaxonomyView.java:174) at com.BiddingSystem.client.BiddingSystem.onModuleLoad(BiddingSystem.java:63) ... 9 more 

3 个答案:

答案 0 :(得分:1)

Anchor a = new Anchor("hi");
a.addClickhandler(new ClickHandler() {
     @Override
     public void onClick(ClickEvent event) {
           Window.alert("hi");
     }

});

答案 1 :(得分:0)

我建议你只使用UIBinder ...看看this tutorial。对于锚点,您将使用<g:Anchor>元素。

答案 2 :(得分:0)

您的HTMLPanel未附加到文档。

您只能将附加的元素传递给wrap()方法。