jQuery focus() 获取焦点

本文介绍了 当元素获得焦点时(当通过鼠标点击选中元素或通过 tab 键定位到元素时),发生 focus 事件。focus() 方法触发 focus 事件,或规定当发生 focus 事件时运行的函数。

当元素获得焦点时(当通过鼠标点击选中元素或通过 tab 键定位到元素时),发生 focus 事件。

focus() 方法触发 focus 事件,或规定当发生 focus 事件时运行的函数。

提示:该方法通常与 blur() 方法一起使用。

语法:

触发被选元素的 focus 事件:
$(selector).focus()

添加函数到 focus 事件:
$(selector).focus(function)

参数

  • function : 可选。规定当 focus 事件发生时运行的函数。

实例:

<html>
<head>
<meta charset="utf-8"> 
<title>IT懒猫 www.catroom.com.cn</title> 
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("input").focus(function(){
    $("span").css("display","inline").fadeOut(2000);
  });
});
</script>
<style>
span
{
display:none;
}
</style>
</head>
<body>

<input>
<span>请输入你的电话号码?</span>
<p>点击输入框获取焦点。</p>

</body>
</html>
上一篇 下一篇


推荐文章

评论
说点什么吧?

发表评论

取消回复
  最新文章