Sample Code for js judgment control to get focus
This article mainly introduces the sample code for the js judgment control to get the focus. If you need it, you can refer to it and hope to help you.
JS judges that an object has obtained the focus document. activeElement. tagName // tagName Tag Name
Instance: Disable the input method when determining whether the body obtains the cursor.
The Code is as follows:
Var act = document. activeElement. tagName. toLowerCase ();
If (act. indexOf ("body ")! =-1 | act. indexOf ("html ")! =-1)
{
Document. body. style. imeMode = 'Disabled ';
}
<Script>
Function test (){
Var srcElem = document. activeElement
Var testval = srcElem. name; // The id and name are commonly used.
Alert ("control" + testval + "Get Focus ");
}
</Script>
<Input name = "a" type = "text" value = "test a" onclick = "test ();">
<Input name = "B" type = "text" value = "test B" onclick = "test ();">