简体   繁体   English

GWT gwtquery .live()方法可能出现的错误

[英]Possible bug with GWT gwtquery .live() method

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. 我需要.live()而不是.bind()因为将生成新的此类链接。 (I know jQuery's .live() is deprecated in favor of .on() , but gwt-query doesn't have a .on() yet.) (我知道不推荐使用jQuery的.live()而推荐使用.on() ,但gwt-query还没有.on() 。)

I defined the handler like this (just as the gwtquery example does ): 我定义了这样的处理程序(就像gwtquery示例所做的那样 ):

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 . 我可以在Chrome开发工具中查看事件监听器: http : //screencloud.net/v/bV5V I think it's on the body because it's a .live() . 我认为它在body上,因为它是.live()

I tried using .bind() and it worked fine. 我尝试使用.bind() ,它工作正常。 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. body事件侦听器在a.link_list中进行了更改,并且处理程序执行了应做的事情,但是(如所记录的那样,我没有测试)不适用于新创建的链接。

I filed a bug for the .live() method, but maybe I'm doing something wrong. 我为.live()方法提交了一个错误 ,但也许我做错了什么。

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. 另外,我不知道没有gwtquery怎么做,GWT似乎没有按类选择元素的方法,也没有将侦听器不断添加到新元素的方法。

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? 您可以发送完整的onModuleLoad代码来演示此错误行为吗?

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 我使用live编写了一个简单的示例,当在Chrome和FF中添加新的gwt小部件或带有gquery的dom元素时,它都可以工作

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;
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM