jQuery :focus 選擇器


jQuery 中的 :focus 選擇器用於選擇當前擁有焦點的元素。

語法

語法如下 −

$(":focus")

示例

現在讓我們來看一個實現 :focus() 選擇器的示例 −

<!DOCTYPE html>
<html>
<head>
<style>
   .one {
      color: brown;
      background-color: orange;
      font-size: 16px;
      border: 2px blue solid;
   }
</style>
<script src="https://code.jquery.com/jquery-3.4.1.js"></script>
<script>
   $(document).ready(function(){
      $("input").focus();
      $(":focus").addClass("one");
   });
</script>
</head>
<h2>Fill the form</h2>
<body>
ID: <input type="text" name="id"><br>
Rank: <input type="text" name="rank"><br>
Fav. Subjects:
Maths: <input type="checkbox" name="sub" value="maths">
English <input type="checkbox" name="sub" value="english"><br>
<input type="submit" value="Submit"><br>
</body>
</html>

輸出

將產生以下輸出 −

更新於:05-11-2019

446 次瀏覽

開啟你的職業生涯

完成課程,獲得認證

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