Dun*_*zzz 5 jquery events onclick event-handling
为了整理我的jQuery,我希望使用事件处理程序为各种元素创建一个"确认"类,这样我就可以启动一个快速确认框,以便用户确认他们的操作.
如果用户点击确认对话框取消,任何下列事件应该被取消,但这应该不会是永久性的,如再次触发事件应该弹出的确认和重新开始.如果我可以解决这个问题,我可以让任何敏感操作都有一个确认问题,以确保用户想要点击.
例如:
$('.confirm').click(function() {
if(!confirm('Are you sure?')) {
// Stop any other click events on $(this) from happening this instance.
}
});
$('.clicker').click(function() {
alert('it happened');
});
<a class="clicker confirm">Click me</a>
Run Code Online (Sandbox Code Playgroud)
期望的输出将是取消确认将取消点击事件,但不完全禁用它.
你可以使用event.stopImmediatePropagation()
jQuery:
$('.confirm').click(function(event) {
if(!confirm('Are you sure?')) {
event.stopImmediatePropagation();
// Stop any other click events on $(this) from happening this instance.
}
});
$('.clicker').click(function() {
alert('it happened');
});
Run Code Online (Sandbox Code Playgroud)