我写了一个确认框使用功能:jQuery的等待事件的返回值
$.fn.confirmation = function(message, color) {
$('.notification').css("background", color);
$('.notification').html(message + "<br/><a href='#' id='sure'>I am sure</a> or <a href='#cancel' id='cancel'>Cancel</a>");
$('.notification').slideDown();
$(document).on("click", '.notification a', function(event){
if($(this).attr("id") == "sure") {
$('.notification').slideUp();
return true;
} else if($(this).attr("id") == "cancel") {
$('.notification').slideUp();
return false;
}
});
};
我的问题是,它会始终返回false,它甚至不等待事件。
我该如何强制JQuery等待用户点击?
为什么你要返回?不要使用'return',使用'event.preventDefault'或'event.stopPropagation'。你是否在萤火虫中使用任何中断点来计算流量? 'return false'表示上面的2,可能不会是你想要的。 – JorgeeFG
@Jorge如果用户点击true或false来执行操作,我是否在函数外部检查? –
它不会返回false,它会返回未定义的(因为'if'语句的目的是同一件事情),因为'$ .fn.confirmation'函数中没有'return'语句。你不能强制这个函数等到用户点击,因为你最终会锁定窗口。 –