Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> HTML

Thuộc tính accessKey của DOM trong HTML


Giới thiệu

Thuộc tính accessKey của DOM (tương ứng thuộc tính HTML accesskey) gán phím tắt để focus/kích hoạt phần tử. Giá trị là một ký tự. Tổ hợp phím phụ thuộc trình duyệt/hệ điều hành.

Tổ hợp phím phổ biến (Windows)

Trình duyệtTổ hợp phím
ChromeAlt + accesskey
FirefoxAlt + Shift + accesskey
EdgeAlt + accesskey
SafariAlt + accesskey

Cú pháp DOM

// Lấy
element.accessKey
// Gán
element.accessKey = "ky_tu"

Ví dụ minh họa

<!DOCTYPE html>
<html>
<body>
  <a id="myid" accesskey="g" href="https://www.google.com/">Google</a>
  <p>Lấy accessKey</p>
  <button onclick="display()">Hiển thị accesskey</button>
  <p id="pid"></p>
  <script>
    function display() {
      var a = document.getElementById("myid").accessKey;
      document.getElementById("pid").innerHTML = a;
    }
  </script>
</body>
</html>

Kết quả hiển thị

Nhấn nút hiển thị "g". Trên Chrome/Edge: Alt+G mở Google.