Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does javascript handle focus

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how to deal with focus in javascript". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how javascript handles focus".

The methods of javascript dealing with focus are as follows: 1, to obtain focus through "field.focus ();" method; 2, to deal with losing focus through "o [I] .blur = function () {...}" method.

This article operating environment: windows7 system, javascript1.8.5 version, DELL G3 computer

How does javascript handle focus?

JavaScript focus processing (get focus focus, lose focus blur)

Focus processing

Focus processing mainly includes focus (gain focus) and blur (loss of focus) event types. The so-called focus is to activate the form field so that it can respond to keyboard events.

Get focus: focus

This event is triggered when you click or use the Tab key to switch to a form element or hyperlink object. The focus event is a way to determine the current mouse position on a page. By default, the entire document is in focus, but clicking or using the Tab key can change the focus.'

Example: after the page is loaded, shift the focus to the first text box field in the form and prepare it to receive user input

Name

Password var form = document.getElementById ("myform"); var field = form.elements ("name"); _ window.onload = function () {field.focus ();}

Losing focus: blur

The blur event type represents the response when the element loses focus, which corresponds to the focus event type and mainly acts on form elements and hyperlinked objects

Example: focus and blur event handlers are bound for all input form elements, setting protruding when the element gains focus and sunken when losing focus

Var o = document.getElementsByTagName ("input"); for (var I = 0; I

< o.length; i++){ o[i].onfocus = function(){ this.style.borderStyle = "outset"; } o[i].blur = function(){ this.style.borderStyle = "inset"; } }

Note: if the field is hidden (

< input type="hidden" >

Or use CSS's display and visibility to hide the field display, and setting its focus will throw an exception.

At this point, I believe you have a deeper understanding of "how to deal with the focus of javascript". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report