jQuery focus() 方法
实例
将函数附加到焦点事件。焦点事件发生在 <input> 字段获得焦点时:
<!DOCTYPE html><html><head><script src="https://libs.baidu.com/jquery/2.1.4/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>
定义与用法
当一个元素获得焦点时(通过鼠标单击或 "选项卡导航" 选择该元素时),就会发生焦点事件。
focus() 方法触发焦点事件,或在焦点事件发生时附加要运行的函数。
提示: 此方法通常与 blur() 方法一起使用。
语法
触发选定元素的焦点事件:
$(selector).focus()
将函数附加到焦点事件:
$(selector).focus(function)
| 参数 | 描述 |
|---|---|
| function | 可选。指定焦点事件发生时要运行的函数 |