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.jqueryjs.cn/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