隐藏元素上的模糊不会移除焦点

时间:2012-09-18 17:59:12

标签: javascript jquery javascript-events backbone.js

我使用Chrome版本21.0.1180.89

我有一个输入.hide()并致电.blur()。模糊事件被触发,输入实际上没有失去焦点。

这是记录在案的行为吗?有一个很好的解决方法吗?

我正在构建一个Backbone应用程序,当用户按Enter键或字段失去焦点时,应保存文本字段中的数据。就像TodoMC Backbone demo

一样

但他们侥幸逃脱,因为他们每次改变时都会从模板中重新构建整个li-eleent。我不想这样做,因为它打破了CSS过渡。

编辑:

大多数相关的Backbone代码:

var app = app || {};

$(function() {
    'use strict';

    // Todo Item View
    // --------------

    // The DOM element for a todo item...
    app.TodoView = Backbone.View.extend({

        //... is a list tag.
        tagName:  'li',

        // Cache the template function for a single item.
        template: _.template( $('#item-template').html() ),

        // The DOM events specific to an item.
        events: {
            'click .toggle':    'togglecompleted',
            'dblclick label':   'edit',
            'click .destroy':   'clear',
            'keypress .edit':   'updateOnEnter',
            'blur .edit':       'close'
        },

        // Switch this view into `"editing"` mode, displaying the input field.
        edit: function() {
            this.$el.addClass('editing');
            this.input.focus();
        },

        // Close the `"editing"` mode, saving changes to the todo.
        close: function() {
            var value = this.input.val().trim();

            if ( value ) {
                this.model.save({ title: value });
            } else {
                this.clear();
            }

            this.$el.removeClass('editing');
            this.$el.addClass('syncing');
        },

        // If you hit `enter`, we're through editing the item.
        updateOnEnter: function( e ) {
            if ( e.which === ENTER_KEY ) {
                // Stupid hack to trigger a blur only once.
                this.$el.append(this.input.detach());
//              this.close();
            }
        },

        // Remove the item, destroy the model from *localStorage* and delete its view.
        clear: function() {
            this.model.destroy({wait: true});
            this.$el.addClass('deleting');
            this.$el.addClass('syncing');
        }
    });
});

0 个答案:

没有答案