4

我使用 Chrome 版本 21.0.1180.89

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

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

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

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

编辑:

大多数相关的主干代码:

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');
        }
    });
});
4

0 回答 0