想要获取焦点需要使用jquery的focus方法。
定义和用法
当元素获得焦点时,发生 focus 事件。
当通过鼠标点击选中元素或通过 tab 键定位到元素时,该元素就会获得焦点。
focus() 方法触发 focus 事件,或规定当发生 focus 事件时运行的函数。
将函数绑定到 focus 事件
语法
$(selector).focus(function)
function:可选。规定当发生 focus 事件时运行的函数。
实例:
锁定焦点
<html><head><script type="text/javascript" src="/jquery/jquery.js"></script><script type="text/javascript">$(document).ready(function(){ $(input).focus(function(){ $(input).css(background-color,#ffffcc); }); });</script></head><body>enter your name: <input type="text" /><p>请在上面的输入域中点击,使其获得焦点,然后就锁定焦点了。</p></body></html>
以上就是jquery怎么锁定焦点的详细内容。
