JS to determine whether the page control gets focus

  • 2020-03-30 01:13:37
  • OfStack

Take a TextBox, for example, and use javascript to determine if it gets the focus.

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

When the textbox is a server-side control, we can easily think of using: if(textbox1.focused) for judgment, and textbox1.attributes. Add ('onfocus',' method to execute '); To bind the method. Of course, server-side controls can also be manipulated using JS, however, it is important to note that the server-side ID and the actual client ID are likely to be different, so be careful when using getelementbyid: document.getelementbyid ('< % % = TextBox1. The ClientID > ').

When the text box for the server controls, you can use the document. The activeElement. Id and TextBox1 comparison, can know whether TextBox1 to focus, example: the if (document. ActiveElement    = =     Document. Form1. ElementName) {}.

Related articles: