jQuery中focus事件用法实例
本文实例讲述了jQuery中focus事件用法。分享给大家供大家参考。具体分析如下:
当元素获得焦点或者调用focus()方法时候会触发focus事件。
一个完整的事件过程,不但要有能够触发事件的条件,而且还要有事件处理程序。
可以通过focus()方法为focus事件绑定事件处理程序。例如:
$("input").focus(function(){$(this).css("backgroundColor","red")})以上代码就是将function函数作为事件处理程序通过focus()方法绑定到focus事件。当触发focus事件的时候,就会调此函数。
实例代码:
<!DOCTYPEhtml>
<html>
<head>
<metacharset="utf-8">
<metaname="author"content="https://www.nhooo.com/"/>
<title>focus事件-毛票票</title>
<scripttype="text/javascript"src="mytest/jQuery/jquery-1.8.3.js"></script>
<scripttype="text/javascript">
$(document).ready(function(){
$("input").focus(function(){
$(this).css("backgroundColor","red")
})
})
</script>
</head>
<body>
<div>
<ul>
<li>用户名:<inputtype="text"name="username"/></li>
<li>密码:<inputtype="password"name="userpassword"/></li>
</ul>
</div>
</body>
</html>
以上代码可以为input元素注册focus事件处理函数,当获得焦点的时候能够设置文本框的背景色。
希望本文所述对大家的jQuery程序设计有所帮助。