Get the App
SLTechnology News&Howtos  ›  Development  › 

How to set the javascript focus event

Shulou Source: shulou.com Published: 2022-06-01 19:50:47 10月04日 Update

This article introduces the relevant knowledge of "how to set javascript focus events". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Focus event: 1, use the focus () method to set the element to get the focus event, the syntax is "$(selector) .focus (function)"; 2, use the blur () method to set the element to lose focus event, the syntax is "$(selector) .blur (function)".

The operating environment of this tutorial: windows10 system, javascript1.8.5 version, Dell G3 computer.

What is the javascript focus event?

The focus event occurs when the element gains focus.

When you select an element with a mouse click or navigate to it with the tab key, the element gets focus.

The focus () method triggers the focus event, or specifies the function to run when the focus event occurs.

The syntax is:

(selector) .focus ()

Or:

$(selector) .focus (function)

The blur event occurs when an element loses focus.

The blur () method triggers the blur event, or specifies the function to run when the blur event occurs.

The syntax is:

(selector) .blur ()

Or:

$(selector) .blur (function)

Examples are as follows:

$(document) .ready (function () {$("input") .focus (function () {$("input"). Css ("background-color", "# FFFFCC");}); $("input") .blur (function () {$("input") .css ("background-color", "# D6D6FF");}); Enter your name:

Please click in the input field above to make it focus, and then click outside the input field to make it lose focus.

Output result:

This is the end of "how to set javascript focus events". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Events focus elements methods grammar content functions more knowledge input output running practical successful learning next dilemma actual situation tutorials articles Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn NVidia Shulou Information Microsoft Huawei