使用委托以特定顺序触发?

时间:2013-04-07 05:15:33

标签: jquery delegates

我要做的是设置它,以便在单击链接时触发警报。是否可以定义首先触发哪个警报?

$("#container").delegate("a.link", "click", function(){                    
    alert("This was message #1");
});

$("#container").delegate("a.link", "click", function(){                    
    alert("This was message #2");
});

1 个答案:

答案 0 :(得分:3)

我在代码中使用的一种方法是为第二个事件监听器设置第二个事件类型:

$("#container").delegate("a.link", "click", function(){                    
    alert("This was message #1");
    $(this).trigger("click-event-2");
});

$("#container").delegate("a.link", "click-event-2", function(){                    
    alert("This was message #2");
});

另外值得一提的是delegate超越了.on()

$("#container").on("click","a.link", function(){                    
    alert("This was message #1");
    $(this).trigger("click-event-2");
});

$("#container").on("click-event-2","a.link", function(){                    
    alert("This was message #2");
});