Sự kiện onkeyup

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

Sự kiện onkeyup xảy ra khi người dùng thả phím (trên bàn phím).

Lưu ý:Thứ tự các sự kiện liên quan đến sự kiện onkeyup:

  1. onkeydown
  2. onkeypress
  3. onkeyup

Mô hình

Ví dụ 1

Chạy JavaScript khi người dùng thả phím:

<input type="text" onkeyup="myFunction()">

Thử ngay

Ví dụ 2

Sử dụng cùng lúc sự kiện "onkeydown" và "onkeyup":

<input type="text" onkeydown="keydownFunction()" onkeyup="keyupFunction()">

Thử ngay

Ví dụ 3

Xuất ra phím thực tế được thả trong trường văn bản:

Nhập tên của bạn: <input type="text" id="fname" onkeyup="myFunction()">
<script>
function myFunction() {
  var x = document.getElementById("fname").value;
  document.getElementById("demo").innerHTML = x;
}
</script>

Thử ngay

Cú pháp

Trong HTML:

<element onkeyup="myScript">

Thử ngay

Trong JavaScript:

object.onkeyup = function(){myScript};

Thử ngay

Trong JavaScript, sử dụng phương thức addEventListener():

object.addEventListener("keyup", myScript);

Thử ngay

Chú ý:Internet Explorer 8 hoặc sớm hơn không hỗ trợ Phương thức addEventListener().

Chi tiết kỹ thuật

Bùng nổ: Hỗ trợ
Có thể hủy: Hỗ trợ
Loại sự kiện: KeyboardEvent
Các thẻ HTML được hỗ trợ: Tất cả các phần tử HTML, trừ:<base>, <bdo>, <br>, <head>, <html>, <iframe>, <meta>, <param>, <script>, <style> và <title>
Phiên bản DOM: Level 2 Events

Hỗ trợ trình duyệt

Sự kiện Chrome IE Firefox Safari Opera
onkeyup Hỗ trợ Hỗ trợ Hỗ trợ Hỗ trợ Hỗ trợ