将ClickHandler与Element的子元素一起使用(Using a ClickHandler with a child of Element)

我需要将具有特定ClickHandler的锚添加到元素中。 但我的Anchor的onClick(...)方法永远不会被调用。

我该如何解决这个问题?

Element th = DOM.createTH(); Anchor link = new Anchor(); link.setText("my link"); link.addClickHandler(new ClickHandler() { public void onClick(ClickEvent event) { Window.alert("Clicked!"); } }); th.appendChild(link.getElement());

I need to add an Anchor with a specific ClickHandler into an Element. But the onClick(...) method of my Anchor is never called.

How can I fix that?

Element th = DOM.createTH(); Anchor link = new Anchor(); link.setText("my link"); link.addClickHandler(new ClickHandler() { public void onClick(ClickEvent event) { Window.alert("Clicked!"); } }); th.appendChild(link.getElement());

最满意答案

我没有尝试过以这种方式实施它,但是,我这样做,它工作正常。

final Element link = DOM.createAnchor(); final Element th = DOM.createTH(); link.setInnerText("my link"); link.setAttribute("style", "cursor:pointer;"); DOM.sinkEvents(link, Event.ONCLICK); DOM.setEventListener(link, new EventListener() { public void onBrowserEvent(Event event) { Window.alert("Clicked!"); } }); th.appendChild(link);

我认为这有助于。

I have not tried implementing it in such way but, I do in this way and it is working properly.

final Element link = DOM.createAnchor(); final Element th = DOM.createTH(); link.setInnerText("my link"); link.setAttribute("style", "cursor:pointer;"); DOM.sinkEvents(link, Event.ONCLICK); DOM.setEventListener(link, new EventListener() { public void onBrowserEvent(Event event) { Window.alert("Clicked!"); } }); th.appendChild(link);

I think this helps.

更多推荐