I'm trying to do the following:
I want to add a specific handler for some links, denoted by a class.
$("a.link_list").live("click", new ListLinkHandler());
I need .live()
instead of .bind()
because new such links will be generated. (I know jQuery's .live()
is deprecated in favor of .on()
, but gwt-query doesn't have a .on()
yet.)
I defined the handler like this (just as the gwtquery example does ):
public class ListLinkHandler extends Function {
@Override
public boolean f(Event e) { [...] }
}
However, the handler method is never called when I click the links. I can see the event listener in Chrome Dev Tools: http://screencloud.net/v/bV5V . I think it's on the body
because it's a .live()
.
I tried using .bind()
and it worked fine. The body
event listener changed in a a.link_list
and the handler does what it's supposed to do, but (as documented, I didn't test) not for newly created links.
I filed a bug for the .live()
method, but maybe I'm doing something wrong.
Also, I have no idea how to do it without gwtquery, GWT doesn't seem to have a method for selecting elements by class, neither to continually add the listener to new elements.
It seems you are doing something wrong, but I need more code to be sure. Could you send the complete onModuleLoad
code which demonstrates this wrong behavior?
I have written a quick example using live
, and it works either when adding new gwt widgets or dom elements with gquery, in both Chrome and FF
public void onModuleLoad() {
$("a.link_list").live("click", new ListLinkHandler());
// Add a new link via gquery
$("<a class='link_list' href=javascript:alert('href') onClick=alert('onClick')>Click </a>").appendTo(document);
// Add a new link via gwt widgets
Anchor a = new Anchor("click");
a.setStyleName("link_list");
a.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
Window.alert("clickHandler");
}
});
RootPanel.get().add(a);
}
public class ListLinkHandler extends Function {
@Override
public boolean f(Event e) {
Window.alert("live");
return true;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.