【问题标题】:Insert a CR/LF into a textarea将 CR/LF 插入文本区域
【发布时间】:2011-08-06 15:04:17
【问题描述】:

我有以下代码:

document.onkeydown=function(e) {
if (e.which == 13 && isCtrl) {
   log('Ctrl CR');
} else if (e.which == 17) {
   isCtrl = true;
};

我需要在光标位于输入文本区域的位置插入回车/换行。 现在想想,我可能应该使用 textarea 选择器而不是 document.onkeydown,但是 $('textarea').onkeydown 不起作用。

【问题讨论】:

  • $('textarea').keydown,没有“开”。
  • 天啊!谢谢!我对必须一起学习 js 和 jQuery 感到困惑。

标签: javascript jquery


【解决方案1】:
$('textarea').keydown(function (e){
    var $this = $(this);
    if (e.which === 13 && e.ctrlKey) {
        $this.val($this.val() + '\r\n'); // untested code (to add CRLF)
    }
});

参考

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-03-14
  • 1970-01-01
  • 1970-01-01
  • 2011-03-07
  • 2013-02-23
  • 2011-03-07
相关资源
最近更新 更多