Adding a click handler for an html element?

前端 未结 2 1643
情歌与酒
情歌与酒 2021-02-10 19:56

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


  
Item A
相关标签:
2条回答
  • 2021-02-10 20:17

    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.

    0 讨论(0)
  • 2021-02-10 20:18

    I haven't tested this, but the general idea is right, and easy enough to extend for more than one target element. You might like to store the elements returned by DOM.getElementById() beforehand to keep things fast. Bear in mind that onPreviewNativeEvent() will be called for every user event, so keep it light.

    Event.addNativePreviewHandler(new NativePreviewHandler() {
        public void onPreviewNativeEvent(NativePreviewEvent event) {
            if (Event.as(event).getTypeInt() == Event.ONCLICK &&
                DOM.isOrHasChild(DOM.getElementById("A"), Element.as(event.getEventTarget()))) {
                // Element 'A' was clicked.
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题