當元素在 HTML 中獲得焦點時執行一個指令碼?


在 HTML 中使用 onfocus 屬性可以在元素獲得焦點後執行一個指令碼。

示例

你可以嘗試執行以下程式碼來實現 onfocus 屬性 −

<!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>

更新於: 01-Jun-2020

420 次瀏覽

開啟你的 職業

完成課程即可獲得認證

立即開始
廣告
© . All rights reserved.