1. 程式人生 > 其它 >js onfocus事件、js onblur事件

js onfocus事件、js onblur事件

技術標籤:Web前端jshtmljavascriptcss

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事件