Hướng dẫn what is charcode in javascript? - charcode trong javascript là gì?

Ví dụ

Nhận unicode của ký tự đầu tiên trong một chuỗi:

hãy để văn bản = "Hello World"; Đặt mã = ​​text.charcodeat (0);
let code = text.charCodeAt(0);

Hãy tự mình thử »

Nhận Unicode của thứ hai:

hãy để văn bản = "Hello World"; Đặt mã = ​​text.charcodeat (1);
let code = text.charCodeAt(1);

Hãy tự mình thử »

Nhận Unicode của thứ hai:


hãy để văn bản = "Hello World"; Đặt mã = ​​text.charcodeat (1);

Thêm ví dụ dưới đây.

Định nghĩa và cách sử dụng

Phương thức charCodeAt() trả về unicode của ký tự tại một chỉ mục được chỉ định (vị trí) trong một chuỗi.

Chỉ số của ký tự đầu tiên là 0, thứ hai là 1, ....

Chỉ số của ký tự cuối cùng là độ dài chuỗi - 1 (xem ví dụ bên dưới).

Xem thêm Phương pháp charAt().

CharCodeat () vs CodePointat ()

charCodeAt() là UTF-16, codePointAt() là Unicode.

charCodeAt() Trả về một số từ 0 đến 65535.


Cả hai phương thức trả về một số nguyên đại diện cho mã UTF-16 của một ký tự, nhưng chỉ codePointAt() có thể trả về toàn bộ giá trị của giá trị Unicode LEGETHER 0xFFFF (65535).

Để biết thêm thông tin về các bộ ký tự Unicode, hãy truy cập tham chiếu Unicode của chúng tôi.

Cú phápThông số
Tham sốSự mô tả
The index (position) of a character.
Default value = 0.

mục lục

Không bắt buộc. Một số. Chỉ số (vị trí) của một ký tự. Giá trị mặc định = 0.Thông số
Tham sốSự mô tả
NaN if the index is invalid.


mục lục

Không bắt buộc. Một số. Chỉ số (vị trí) của một ký tự. Giá trị mặc định = 0.

Giá trị trả về
let code = text.charCodeAt(text.length-1);

Hãy tự mình thử »

Loại hình

Một số
let code = text.charCodeAt(15);

Hãy tự mình thử »


Unicode của ký tự tại chỉ số được chỉ định. Nan nếu chỉ số không hợp lệ.

Nhiều ví dụ hơn

Nhận unicode của ký tự cuối cùng trong một chuỗi:

hãy để văn bản = "Hello World"; Đặt mã = ​​text.charcodeat (text.length-1);Nhận Unicode của ký tự thứ 15: hãy để văn bản = "Hello World"; Đặt mã = ​​text.charcodeat (15);Hỗ trợ trình duyệtcharCodeAt() là tính năng ECMAScript1 (ES1).ES1 (JavaScript 1997) được hỗ trợ đầy đủ trong tất cả các trình duyệt:
Trình duyệt ChromeTrình duyệt ChromeTrình duyệt ChromeTrình duyệt ChromeTrình duyệt ChromeTrình duyệt Chrome


Dành: Tính năng này không còn được khuyến nghị. Mặc dù một số trình duyệt vẫn có thể hỗ trợ nó, nhưng nó có thể đã bị xóa khỏi các tiêu chuẩn web có liên quan, có thể đang trong quá trình bị loại bỏ hoặc chỉ có thể được giữ cho mục đích tương thích. Tránh sử dụng nó và cập nhật mã hiện có nếu có thể; Xem bảng tương thích ở cuối trang này để hướng dẫn quyết định của bạn. Xin lưu ý rằng tính năng này có thể ngừng hoạt động bất cứ lúc nào. This feature is no longer recommended. Though some browsers might still support it, it may have already been removed from the relevant web standards, may be in the process of being dropped, or may only be kept for compatibility purposes. Avoid using it, and update existing code if possible; see the compatibility table at the bottom of this page to guide your decision. Be aware that this feature may cease to work at any time.

Thuộc tính chỉ đọc charCode của giao diện

const input = document.querySelector('input');
const log = document.querySelector('#log');

input.addEventListener('keypress', (e) => {
  log.innerText = `Key pressed: ${String.fromCharCode(e.charCode)}\ncharCode: ${e.charCode}`;
});
0 trả về giá trị Unicode của khóa ký tự được nhấn trong sự kiện
const input = document.querySelector('input');
const log = document.querySelector('#log');

input.addEventListener('keypress', (e) => {
  log.innerText = `Key pressed: ${String.fromCharCode(e.charCode)}\ncharCode: ${e.charCode}`;
});
1.charCode read-only property of the
const input = document.querySelector('input');
const log = document.querySelector('#log');

input.addEventListener('keypress', (e) => {
  log.innerText = `Key pressed: ${String.fromCharCode(e.charCode)}\ncharCode: ${e.charCode}`;
});
0 interface returns the Unicode value of a character key pressed during a
const input = document.querySelector('input');
const log = document.querySelector('#log');

input.addEventListener('keypress', (e) => {
  log.innerText = `Key pressed: ${String.fromCharCode(e.charCode)}\ncharCode: ${e.charCode}`;
});
1 event.

CẢNH BÁO: Không sử dụng thuộc tính này, vì nó không được chấp nhận. Thay vào đó, hãy lấy giá trị Unicode của ký tự bằng thuộc tính

const input = document.querySelector('input');
const log = document.querySelector('#log');

input.addEventListener('keypress', (e) => {
  log.innerText = `Key pressed: ${String.fromCharCode(e.charCode)}\ncharCode: ${e.charCode}`;
});
2. Do not use this property, as it is deprecated. Instead, get the Unicode value of the character using the
const input = document.querySelector('input');
const log = document.querySelector('#log');

input.addEventListener('keypress', (e) => {
  log.innerText = `Key pressed: ${String.fromCharCode(e.charCode)}\ncharCode: ${e.charCode}`;
});
2 property.

Giá trị

Một số đại diện cho giá trị unicode của khóa ký tự đã được nhấn.

Ví dụ

HTML

<p>Type anything into the input box below to log a <code>charCodecode>.p>
<input type="text" />
<p id="log">p>

JavaScript

const input = document.querySelector('input');
const log = document.querySelector('#log');

input.addEventListener('keypress', (e) => {
  log.innerText = `Key pressed: ${String.fromCharCode(e.charCode)}\ncharCode: ${e.charCode}`;
});

Kết quả

Ghi chú

  • Trong một sự kiện
    const input = document.querySelector('input');
    const log = document.querySelector('#log');
    
    input.addEventListener('keypress', (e) => {
      log.innerText = `Key pressed: ${String.fromCharCode(e.charCode)}\ncharCode: ${e.charCode}`;
    });
    
    1, giá trị unicode của khóa được lưu trữ trong thuộc tính
    const input = document.querySelector('input');
    const log = document.querySelector('#log');
    
    input.addEventListener('keypress', (e) => {
      log.innerText = `Key pressed: ${String.fromCharCode(e.charCode)}\ncharCode: ${e.charCode}`;
    });
    
    4 hoặc charCode, nhưng không bao giờ cả hai. Nếu phím nhấn tạo ra một ký tự (ví dụ: 'A'), charCode sẽ được đặt thành mã của ký tự đó; charCode tôn trọng trường hợp chữ cái (nói cách khác, charCode có tính đến liệu khóa Shift có được giữ không). Mặt khác, mã của phím nhấn được lưu trữ trong
    const input = document.querySelector('input');
    const log = document.querySelector('#log');
    
    input.addEventListener('keypress', (e) => {
      log.innerText = `Key pressed: ${String.fromCharCode(e.charCode)}\ncharCode: ${e.charCode}`;
    });
    
    4.
  • Khi nhấn một hoặc nhiều khóa sửa đổi, có một số quy tắc phức tạp cho charCode. Xem sự kiện Keypress Gecko để biết chi tiết.
  • charCode không bao giờ được đặt trong các sự kiện charCodeAt()2 và charCodeAt()3. Trong những trường hợp này,
    const input = document.querySelector('input');
    const log = document.querySelector('#log');
    
    input.addEventListener('keypress', (e) => {
      log.innerText = `Key pressed: ${String.fromCharCode(e.charCode)}\ncharCode: ${e.charCode}`;
    });
    
    4 được đặt thay thế.
  • Để có được mã của khóa bất kể nó được lưu trữ trong
    const input = document.querySelector('input');
    const log = document.querySelector('#log');
    
    input.addEventListener('keypress', (e) => {
      log.innerText = `Key pressed: ${String.fromCharCode(e.charCode)}\ncharCode: ${e.charCode}`;
    });
    
    4 hay charCode, truy vấn thuộc tính charCodeAt()7.
  • Các ký tự được nhập thông qua IME không đăng ký thông qua
    const input = document.querySelector('input');
    const log = document.querySelector('#log');
    
    input.addEventListener('keypress', (e) => {
      log.innerText = `Key pressed: ${String.fromCharCode(e.charCode)}\ncharCode: ${e.charCode}`;
    });
    
    4 hoặc charCode.
  • Đối với danh sách các giá trị charCode được liên kết với các khóa cụ thể, hãy chạy Ví dụ 7: Hiển thị các thuộc tính đối tượng sự kiện và xem bảng HTML kết quả.

Thông số kỹ thuật

Sự chỉ rõ
Sự kiện UI # Dom-KeyboardEvent-Charcode
# dom-keyboardevent-charcode

Tính tương thích của trình duyệt web

Bảng BCD chỉ tải trong trình duyệt

Keycode và charcode là gì?

Nếu phím nhấn tạo ra một ký tự (ví dụ: 'A'), charcode được đặt thành mã của ký tự đó;Charcode tôn trọng trường hợp chữ cái (nói cách khác, charcode có tính đến việc khóa phím có được giữ không).Mặt khác, mã của phím nhấn được lưu trữ trong KeyCode. (in other words, charCode takes into account whether the shift key is held down). Otherwise, the code of the pressed key is stored in keyCode .

Charcode 45 là gì?

Các charcodes JavaScript, mà bạn kiểm tra trong một sự kiện Keypress, là ASCII.109 là mã khóa chính xác, nếu được sử dụng trong sự kiện Keydown hoặc KeyUP."-" Có một mã charcode là 45.

E keycode === 13 là gì?

KeyCode 13 là phím Enter.Enter key.

Charcode cho DOT là gì?

Theo trang web, mã khóa cho.là 190.190.