onfocus
定义和用法
onfocus
属性在元素获得焦点时触发
onfocus
通常用于<input>
、<select>
、和<a>
元素。
提示:与onfocus
属性相反的属性事件为onblur
。
注意:onfocus
属性不能使用于以下元素:<base>
、<bdo>
、<br>
、<head>
、<html>
、<iframe>
、<meta>
、<param>
、<script>
、<style>
、或<title>
。
语法
<element onfocus="script">
浏览器支持
![]() | ![]() | ![]() | ![]() | ![]() |
所有主流浏览器都支持onfocus 事件属性 |
示例
<html> <head> <title>www.lanmper.cn</title> <script type="text/javascript"> function setStyle(x) { document.getElementById(x).style.background="yellow" } </script> </head> <body> First name: <input type="text" onfocus="setStyle(this.id)" id="fname" /> <br /> Last name: <input type="text" onfocus="setStyle(this.id)" id="lname" /> </body> </html>
HTML 4.01 与 HTML5之间的差异
无。
属性值
值 | 描述 |
---|---|
script | 规定该onfocus 事件触发时执行的脚本 |
实例
当输入域获得焦点时运行脚本:<input type="text" id="fname" onfocus="setstyle(this.id)">