将 ClickHandler 添加到包含许多其他小部件的 div [英] Adding ClickHandler to div which contains many other widget

查看:24
本文介绍了将 ClickHandler 添加到包含许多其他小部件的 div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Panel 似乎不是从 HasClickHandlers 继承的,我无法向 DivElement 添加 clickHandler.有没有办法将 clickHandler 添加到 DIV?

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?

我不能使用 Label 的原因是我想要点击的 DIV 包含其他 DIV.

The reason I can't use Label as that the DIV I want to be clickable contains other DIV.

推荐答案

默认情况下,Panel 不会接收"onCLick 事件.也就是说,单击面板实际上不会导致ClickEvent",因此不会触发处理程序.要将单击事件与 Panel(或就此而言任何其他小部件)一起使用,您必须首先使用适当的事件位对其调用 sinkEvents().

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.

例如:

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());

上面的代码首先使用 p.sinkEvents(Event.ONCLICK); 为 SimplePanel '启用'点击事件,然后继续向其添加标准 ClickHandler.请注意,即使 SimplePanel 没有实现 HasClickHandlers,您始终可以使用 addHandler() 方法添加处理程序.

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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