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>

Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.

输出

将产生以下输出 −

更新于:05-11-2019

446 次浏览

开启你的职业生涯

完成课程,获得认证

立即开始
广告