问题描述
看起来Panel没有从HasClickHandlers继承,我不能将一个clickHandler添加到DivElement。有没有办法将clickHandler添加到DIV?编辑:我不能使用标签的原因是我想要点击的DIV包含其他DIV。默认情况下,Panel不会沉入onCLick事件。也就是说,点击Panels实际上并不会产生一个'ClickEvent',因此处理程序不会被触发。要在面板中使用点击事件(或者对于其他任何Widget),必须首先使用适当的事件位调用其上的sinkEvents()。
例如:
SimplePanel p = new SimplePanel();
p.sinkEvents(Event.ONCLICK);
p.setTitle(Click me);
p.setSize(600px,600px);
p.addHandler(new ClickHandler(){
@Override
public void onClick(ClickEvent event){
Window.alert(SimplePanel clicked !);
}
},ClickEvent.getType());
上面这段代码首先使用启用SimplePanel的单击事件, p.sinkEvents(Event.ONCLICK);
然后继续将标准ClickHandler添加到它。请注意,尽管SimplePanel不实现HasClickHandler,但您始终可以使用 addHandler()
方法添加处理程序。
It seems that Panel doesn't inherit from HasClickHandlers and I can't add a clickHandler to DivElement. Is there any way to add the clickHandler to a DIV?
EDIT: The reason I can't use Label as that the DIV I want to be clickable contains other DIV.
By default, Panel doesnt 'sink' the onCLick event. That is, clicking on Panels doesnt actually result into a 'ClickEvent' and hence the handler isnt fired. To use click events with Panel (or for that matter any other Widget) you must first invoke sinkEvents() on it with the appropriate event bits.
For example:
SimplePanel p = new SimplePanel();
p.sinkEvents(Event.ONCLICK);
p.setTitle("Click me");
p.setSize("600px", "600px");
p.addHandler(new ClickHandler(){
@Override
public void onClick(ClickEvent event) {
Window.alert("SimplePanel clicked!");
}
}, ClickEvent.getType());
The above piece of code first 'enables' click events for the SimplePanel using p.sinkEvents(Event.ONCLICK);
and then goes on to add the standard ClickHandler onto it. Note that even though SimplePanel doesnt implement HasClickHandlers, you can always add a handler using the addHandler()
method.
这篇关于将ClickHandler添加到包含许多其他小部件的div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!