Home >Web Front-end >JS Tutorial >what is focus in javascript

what is focus in javascript

青灯夜游
青灯夜游Original
2021-10-25 15:54:092428browse

In JavaScript, the focus is the area of ​​concern, that is, the position where the cursor is currently activated. The small vertical line flashing in the page screen indicates that a control on the web page is selected and can be operated; it can be operated through focus () method to obtain the focus, and the blur() method to remove the focus.

what is focus in javascript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

What is focus in javascript?

In JavaScript, the focus is the area of ​​concern, that is, the position where the cursor is currently activated. The small vertical line flashing in the page screen indicates that a certain control on the web page is selected. You can being manipulated. Click the mouse to get the cursor, and the Tab key can switch focus according to the set Tabindex.

For example, if a text box gets the focus, the characters you type on the keyboard will directly enter the text box; or if a drop-down list box gets the focus, if you press the down arrow on the keyboard, it will A list will be made. The program also has events that occur when focus is gained (gotfocus()), events that occur when focus is lost (lostfocus()), and a method to set focus for the control (setfocus()). Making good use of focus can make your program appear very user-friendly.

Obtains focus and can affect the area, including Activity interface and visual controls, etc., and enables the area to automatically perform an action (such as scrolling and animation) or a certain logical event to occur; In addition, you can also manually execute click events and operate the focused area. Whether this area (Activity interface, controls, etc.) has any function and response depends on whether it has acquired focus.

Methods for JavaScript to acquire focus and remove focus:

JavaScript can obtain focus through the focus() method and remove focus through the blur() method.

Get focus:

The focus() method is used to give focus to the checkbox.

Syntax:

checkboxObject.focus()

Lost focus:

blur() method is used to remove the focus from the radio button.

Syntax;

radioObject.blur()

Example:

The following example sets or removes the focus on a radio button:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script type="text/javascript">
function setFocus() {
document.getElementById(&#39;check1&#39;).focus()
}
function loseFocus() {
document.getElementById(&#39;check1&#39;).blur()
}
</script>
</head>
<body>
<form>
<input type="text" id="check1" />
<br /><br />
<input type="button" onclick="setFocus()" value="获取焦点" />
<input type="button" onclick="loseFocus()" value="移开焦点" />
</form>
</body>
</html>

Rendering:

what is focus in javascript

[Recommended learning: javascript advanced tutorial]

The above is the detailed content of what is focus in javascript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn