HTML onfocus 事件屬性


HTML onfocus 事件屬性在 HTML 文件中的 HTML 元素獲得焦點時使用。

語法

以下是語法 −

<tagname onfocus=”script”></tagname>

示例

讓我們看一個關於 HTML onfocus 事件屬性的示例 −

實際演示

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
   body {
      color: #000;
      height: 100vh;
      background: linear-gradient(62deg, #FBAB7E 0%, #F7CE68 100%) no-repeat;
      text-align: center;
      padding: 20px;
   }
   textarea {
      border: 2px solid #fff;
      background: transparent;
      font-size: 1rem;
   }
   ::placeholder {
      color: #000;
      font-size: 1rem;
   }
</style>
</head>
<body>
<h1>HTML onfocus Event Attribute Demo</h1>
<textarea placeholder="Enter your message here" onfocus="focusFn()" rows='8' cols="50"></textarea>
<script>
   function focusFn() {
      document.querySelector('textarea').style.background = '#ffffff36';
   }
</script>
</body>
</html>

輸出

現在在文字區域中輸入您的訊息,以觀察 onfocus 事件屬性的工作方式。

更新時間:16-2 月-2021

145 瀏覽次數

開啟您的 職業生涯

透過完成課程獲得認證

開始
廣告