js onfocus事件、js onblur事件

onfocus事件:
html元素获取到焦点所触发的事件,通常用于 <input>输入框标签,<select>下拉列表标签,以及<a>超链接标签,不是所有html元素都支持onfocus事件。

onblur事件:
html元素失去焦点所触发的事件,与onfocus事件相对,通常用于<input>输入框标签,不是所有html元素都支持onblur事件。

代码展示:

<html>

<head>
    <script type="text/javascript">
        function focus_input() {
            document.getElementById("first").style.backgroundColor = "purple";
        }
        function blur_input() {
            document.getElementById("first").style.backgroundColor = "gold";
        }
        function focus_select() {
            document.getElementById("second").style.backgroundColor = "purple";
        }
        function blur_select() {
            document.getElementById("second").style.backgroundColor = "gold";
        }
        function focus_a() {
            document.getElementById("third").style.backgroundColor = "purple";
        }
        function blur_a() {
            document.getElementById("third").style.backgroundColor = "gold";
        }
    </script>
</head>

<body>
    <input id="first" type="text" onfocus="focus_input()" onblur="blur_input()" />
    <select id="second" onfocus="focus_select()" onblur="blur_select()">
        <option value="option_1">option_1</option>
        <option value="option_2">option_2</option>
        <option value="option_3">option_3</option>
    </select>
    <a id="third" href="#" onfocus="focus_a()" onblur="blur_a()">a标签</a>
</body>

</html>

效果展示:
js onfocus事件、js onblur事件

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值