委派事件两次发射

Sar*_*pps 5 jquery delegates

此委托事件触发两次(有时不总是).

client.bindButtonClickFunction = function(){

    $("#client-plugin").delegate(".client-button", "click", function()
    {
        var id = this.id.split('-')[2];
        client.retrieveMessageByID(id);
    });
};
Run Code Online (Sandbox Code Playgroud)

插入所有".client-button"后我调用该函数.

有关如何阻止它的任何想法?我尝试了event.stopPropagation(),并且还没有授权和重新委托无效.

这是在Chrome中,作为Chrome插件的一部分.

Mrc*_*ief 21

根据您注册代理的方式,您可能希望:

$("#client-plugin").undelegate('event').delegate('event', ...)
Run Code Online (Sandbox Code Playgroud)

另外,尝试return false从处理程序中添加一个.


Ran*_*ana 10

你需要立即停止传播

   $("#client-plugin").on(".client-button", "click", function (e) {
    e.stopImmediatePropagation();
    var id = this.id.split('-')[2];
    client.retrieveMessageByID(id);
});
Run Code Online (Sandbox Code Playgroud)