Adding a click handler for an html element?

前端 未结 2 1645
情歌与酒
情歌与酒 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: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.
            }
        }
    }
    

提交回复
热议问题