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){}.