javascript jQuery event.which 在 Enter 键按下时返回 13

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11179556/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 12:17:54  来源:igfitidea点击:

jQuery event.which returns 13 on Enter key press

javascriptjqueryline-breaks

提问by collimarco

Why event.which doesn't return 13 (CR) or 10 (LF) depending on the operating system?

为什么 event.which 不返回 13 (CR) 或 10 (LF) 取决于操作系统?

$('#something').on('keypress', function (event) {
  switch (event.which) {
  case 13:
    ...
    break;
  }    
});

UPDATE

更新

I know that Carriage return is equal to 13 in ASCII code. But why Carriage return is common to all operating systems but produces different outputsin text (i.e. LF, CR, CR LF)?

我知道回车在 ASCII 代码中等于 13。但为什么回车对所有操作系统都是通用的,但在文本中产生不同的输出(即 LF、CR、CR LF)?

回答by gdoron is supporting Monica

Because that is the ASCII code for return...

因为那是返回的 ASCII 码...

The table:

桌子:

enter image description here

在此处输入图片说明

Look at number 13:

看数字13

13    015     0D     00001101     CR     
    Carriage Return

13    015 0D 00001101 CR     
    回车

Read this:

读这个:

The actual codes representing a newline vary across operating systems, which can be a problem when exchanging text files between systems with different newline representations.

表示换行符的实际代码因操作系统而异,这在具有不同换行表示的系统之间交换文本文件时可能会出现问题。

Making the value fixed no matter which OS is being used, makes our life a lot easier and letting us focus on the real issues we need to do.

无论使用哪种操作系统,都将价值固定,让我们的生活更轻松,让我们专注于我们需要做的实际问题。