我有以下代码:
document.onkeydown=function(e) {
if (e.which == 13 && isCtrl) {
log('Ctrl CR');
} else if (e.which == 17) {
isCtrl = true;
};
我需要在光标位于输入文本区域中的位置插入回车/换行。 现在想想,我可能应该使用文本区域选择器而不是 document.onkeydown,但是 $('textarea').onkeydown 不起作用。
最佳答案
$('textarea').keydown(function (e){
var $this = $(this);
if (e.which === 13 && e.ctrlKey) {
$this.val($this.val() + '\r\n'); // untested code (to add CRLF)
}
});
引用
关于javascript - 将 CR/LF 插入文本区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5682088/