当检测到事件'keydown'时,也会自动触发事件'blur'。如果触发了“keydown”事件,如何防止元素上的“模糊”事件?这是jsFiddle和代码:HTML:
<div id="delegatee">
<span id="ctrl" contenteditable="true"></span>
</div>
CSS:
#ctrl {
width:100px;
height:50x;
border:1px solid black;
display:inline-block;
}
JavaScript:
function saveOrDelete() {
$("#ctrl").attr('contentEditable', false);
alert('Triggered'); // alerts 2 times
}
$("#delegatee").on('keydown blur', '#ctrl', saveOrDelete)
请注意,此解决方案不起作用:
function saveOrDelete() {
$("#delegatee").off('keydown blur', '#ctrl', saveOrDelete)
$("#ctrl").attr('contentEditable', false);
alert('Trigger');
$("#delegatee").on('keydown blur', '#ctrl', saveOrDelete)
}