簡體   English   中英

當用戶進入該div時,如何按鍵事件?

[英]How to do press key event when user will enter into that div?

當用戶按下我的contenteditable div時,我想自動按向下箭頭鍵,這是我嘗試過的代碼

$('body').on('keypress', '.chat_txt', function(e) {
       if (e.keyCode === 13) {
            e.preventDefault();
            e.stopPropagation();
            $(this).append('\n'); 
            $(this).trigger({ type: 'keypress', which: 40});
        }
});

但不幸的是,這段代碼毫無結果。
JsFiddle https://jsfiddle.net/2q9x4xzm/

利用以下內容

var e = $.Event('keypress');
        e.which = 40;
        $('.chat_txt').trigger(e);

完整的代碼

    $('body').on('keypress', '.chat_txt', function(e) {
console.log(e.keyCode);
       if (e.keyCode === 13) {

            var evt = $.Event('keypress');
            evt.keyCode = 40;
            console.log(evt);
            $('.chat_txt').trigger(evt);
            e.preventDefault();
            e.stopPropagation();
        }
});

})

的jsfiddle

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM