添加一个HTML元素的点击处理程序? [英] Adding a click handler for an html element?

查看:80
本文介绍了添加一个HTML元素的点击处理程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用html为我预生成了一个页面,它看起来像一个可滚动的div列表,如下所示:

 << ; HTML> 
< div id =a>
< div>商品A< / div>
< / div>
< div id =b>
< div>项目B< / div>
< / div>
< / html>

我想在我的入口点方法中抓住它们,并为每个方法添加一个点击处理程序。我无法弄清楚如何做到这一点。我有类似的东西:

  public void onModuleLoaded(){
RootPanel rp1 = RootPanel.get(a) ;
rp1.addClickHandler(...); //不能这样做。
}

如何在GWT中监听其中某个项目的点击?有没有办法我可以安装一个全局的点击处理程序,只是看点击项目的ID和过滤器?我不一定需要为每个元素添加点击处理程序(如果您有许多元素需要点击处理程序,我认为文档建议您不要这样做),

谢谢

谢谢

解决方案

父元素已经是一个小部件,包装是不允许的。你仍然可以这样做,但是如果你在开发模式下运行应用程序,断言将失败,停止应用程序。



正确(但是单调乏味,在我的意见不完整)方式类似于

  Element elem = DOM.getElementById(billing-component); 
DOM.sinkEvents(elem,Event.ONCLICK | Event.ONMOUSEOUT | Event.ONMOUSEOVER);
DOM.setEventListener(elem,new EventListener(){
@Override
public void onBrowserEvent(Event event){
if(Event.ONCLICK == event.getTypeInt()) {
...
}
}
});

我知道看起来不太好,实际上并不是因为您只能附加一张监听元素并且必须检查事件类型。


I have a page pregenerated for me using html, it looks like a scrollable list of divs, something like:

<html>
  <div id="a">
     <div>Item A</div>
  </div>
  <div id="b">
     <div>Item B</div>
  </div>
</html>

I'd like to grab them in my entry point method, and add a click handler to each. I can't figure out how to do that. I have something like:

public void onModuleLoaded() {
    RootPanel rp1 = RootPanel.get("a");
    rp1.addClickHandler(...); // can't do this.
}

how can I listen for a click on one of those items in GWT? Is there a way I can just install a global click handler and just watch for the ID of clicked items and filter on that? I don't necessarily need to add a click handler for each element (and I think the docs recommend against this if you have many elements which require click handlers),

Thanks

Thanks

解决方案

The problem using wrap() is that if the parent element is already a widget, the wrapping is not allowed. You can still do it and will work, but if you run the application in development mode the assertion will fail, stopping the application.

The right (but tedious and in my opinion incomplete) way is something like

Element elem = DOM.getElementById("billing-component");
DOM.sinkEvents(elem, Event.ONCLICK | Event.ONMOUSEOUT | Event.ONMOUSEOVER);
DOM.setEventListener(elem, new EventListener() {
    @Override
    public void onBrowserEvent(Event event) {
        if (Event.ONCLICK == event.getTypeInt()) {
            …
        }
    }
});

I know doesn't look nice, and actually it isn't because you can only attach a single listener to the element and have to check for the event type.

这篇关于添加一个HTML元素的点击处理程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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