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>輸出
將產生以下輸出 −

廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP