Home  >  Article  >  Web Front-end  >  How to handle form focus events

How to handle form focus events

一个新手
一个新手Original
2017-09-19 10:23:402220browse

Today we will take the text width as an example to talk about the focus event of the form. Other form types are similar

Focus: In order to distinguish the object input by the user, the browser can accept user input when an element has focus.

We can set focus to elements in some ways

1.tab

2, click

##3.js

Not all elements All can accept focus, and only elements that can respond to user operations have focus, such as forms, a tags

1.tab Needless to say, the browser gives us Done, we can use the tab key to change the focus at any time

2.Click to get focus onfocus

When we click on the form element, the element you clicked will gain focus and trigger onfocus

var aInp=document.getElementsByTagName('input');
            for(var i=0;i<aInp.length;i++){
                aInp[i].onfocus=function(){
                    this.value=&#39; &#39;;
                }
            };


Lost focus onblur

Usage: When we click outside. Our form loses focus and triggers onblur

3.js to gain focus. Use the focus method to gain focus, blur Method loses focus

aInp[1].focus();

Usage: Object.focus At this time, the default focus is the second form (same as blur)

**Finally, a method is givenselect

select all the text content in the specified () element (the element that the user can input)---->Realize the copy function

The above is the detailed content of How to handle form focus events. 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