简体   繁体   English

如何从元素中取消绑定焦点事件

[英]How to unbind focusout event from an element

A peculiar problem. 一个特殊的问题。

The script does something on 'mousedown' then executes on either event 'keypress', 'focusout', 'keyup'. 脚本在'mousedown'上执行某些操作,然后在'keypress','focusout','keyup'事件上执行。

The question is how do I kill two remaining events once one of them is executed. 问题是,一旦执行其中一个事件,我如何杀死剩余的两个事件。

I've tried $(document).off() , $(this).off('focusout') , and nothing works. 我尝试过$(document).off()$(this).off('focusout') ,没有任何效果。

Any clues? 有线索吗?

.on('mousedown', '.task-content', function() {
    // Make DIV Editable
})

.on('keypress', '.task-content', function (e) {
    if (e.which == 13 && !e.shiftKey) {
        // Update DIV content to DB
    }
})

.on('focusout', '.task-content', function() {
    // Update DIV content to DB
})

.on('keyup', '.task-content', function (e) {
    if (e.which == 27) {
    // Return DIV to previous state
  }
})

Your code is working fine, maybe you might point a wrong selector . 你的代码工作正常,也许你可能指向一个错误的selector

$('.task-content').on('keypress', function (e) {
    if (e.which == 13 && !e.shiftKey) {
        // Update DIV content to DB
        alert("key press");
        $(this).off('focusout');
    }
})

$('.task-content').on('focusout', function () {
    // Update DIV content to DB
    alert("focus out ");
})

Check this JSFiddle . 检查这个JSFiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM