Home  >  Article  >  Web Front-end  >  Determine whether the page control has gained focus through JS_javascript skills

Determine whether the page control has gained focus through JS_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:05:341000browse

Take a TextBox as an example and use javascript to determine whether it has gained focus.

In fact, it is very simple. The normal thinking is divided into two ways:

When the text box is a server-side control, we can simply think of using: if(TextBox1.Focused) to make a judgment, and use TextBox1.Attributes.add('onfocus','Method to be executed'); to bind the method. Of course, server-side controls can also be operated using JS. However, it should be noted that the server-side ID and the actual client-side ID are likely to be different, so you need to pay attention when using getelementbyid: document.getElementById(' <%= TextBox1.ClientID%>').

When the text box is a server-side control, you can compare document.activeElement.id with TextBox1 to know whether TextBox1 is the focus, for example: if(document.activeElement == document.form1.elementName){}.

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