Computer >> Máy Tính >  >> Lập trình >> HTML

Thực thi một tập lệnh khi phần tử được lấy tiêu điểm trong HTML?


Sử dụng onfocus để thực thi một tập lệnh khi phần tử được lấy tiêu điểm trong HTML.

Ví dụ

Bạn có thể thử chạy mã sau để triển khai onfocus thuộc tính -

<!DOCTYPE html>
<html>
   <body>
      <p>Enter subject name below,</p>
      Subject: <input type = "text" id = "sname" onfocus = "display(this.id)">
      <br>

      <script>
         function display(val) {
            document.getElementById(val).style.background = "blue";
         }
      </script>
   </body>
</html>