我该怎么做
HTML
<button id="onclickpopupmenu">Test</button>
<button id="popupmenuok"></button>
的javascript:
$("#onclickpopupmenu").bind("click",function(){
alert("this can execute multiple times")
$("#popmenuok").bind("click",function(){
alert("this has to be triggered only once eventhough the parent event trigger multiple times")
})
})
请帮帮我......
答案 0 :(得分:2)
只绑定处理程序一次:
var popupmenuBound = false;
$("#onclickpopupmenu").bind("click",function(){
alert("this can execute multiple times");
if (!popupmenuBound) {
$("#popupmenuok").bind("click",function(){
alert("this has to be triggered only once eventhough the parent event trigger multiple times");
});
popupmenuBound = true;
}
});
答案 1 :(得分:2)
有几种方法可以解释你想要的东西。
第一个是第一个按钮的多次点击,这会导致单个 - 但持久(它对每次点击做出反应) - 第二个按钮上的事件处理程序。要做到这一点,你可以这样做:
$('#onclickpopupmenu').click(function() {
// alert
$('#popupmenuok').off('click').on('click', function() {
// alert
});
});
第二个是第一个按钮上的多次单击,每次单击就会在第二个按钮上获得一次性事件处理程序。因此,如果您单击第一个按钮一次,然后单击第二个按钮,您将收到警报,但第二次单击第二个按钮不会执行任何操作。要做到这一点:
$('#onclickpopupmenu').click(function() {
// alert
$('#popupmenuok').off('click').one('click', function() {
// alert
});
});
答案 2 :(得分:0)
$("#onclickpopupmenu").bind("click",function(){
alert("this can execute multiple times");
});
$("#popupmenuok").one("click",function(){
alert("this has to be triggered only once eventhough the parent event trigger multiple times");
});