JQuery click事件在第二次单击时触发,但不会在第一次触发时触发

时间:2016-02-22 17:08:28

标签: javascript jquery html events

我已经谷歌了,但我无法找到解决问题的方法。我在js函数上有一些事件(立即函数调用)。因此,点击事件效果不佳。它仅在第二次调用时触发。

键入长文本时,前三个字段(To,Cc和Cco)应该展开。当您单击“主题”字段时,单击事件应折叠“收件人”,“抄送”和“Cco”字段。它有效,但仅在我第二次点击主题时。

Heres my js(IIF)

function initTextareaEvents(){
    $('section textarea').on({

        focusin:function( ev ) {
            if($( this ).closest( '.info-box' ).length > 0){
                $( this ).elastic();
            }
        },

        keypress:function( ev ) {
            var key = ev.which;
            if(key == 13 || key == 32){
                ev.preventDefault();
                var str = $(  this ).val().trim();
                str += ', ';
                $( this ).val( str );
            }
        },

        click:function ( ev ) {
            if($( this ).closest( '.info-box' ).length === 0){
                $( '#to, #cc, #cco' ).css( 'height', 'auto' );
            }      
        }

    });
}

这是Codepen

1 个答案:

答案 0 :(得分:1)

第一次点击是" Focus Out"我认为。尝试添加像

这样的东西
        focusout:function( ev ) {
            if($( this ).closest( '.info-box' ).length > 0){
                $( this ).elastic();
            }
        },

这似乎让你更接近你想要的行为。