简体   繁体   English

当用户进入该div时,如何按键事件?

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

I want to press arrow down key automatically when user presses enter on my contenteditable div and here is my code i have tried 当用户按下我的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});
        }
});

But unfortunately this code is fruitless. 但不幸的是,这段代码毫无结果。
JsFiddle https://jsfiddle.net/2q9x4xzm/ JsFiddle https://jsfiddle.net/2q9x4xzm/

Make use of the following 利用以下内容

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

Complete code 完整的代码

    $('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 的jsfiddle

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

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