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


使用 onfocus 屬性,在元素獲得焦點時,在 HTML 中執行指令碼。

示例

你可以嘗試執行以下程式碼來實現 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>

更新於: 2020 年 6 月 1 日

240 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.