Sample code for the js judgment control to get focus

  • 2020-03-30 02:09:53
  • OfStack

JS judgment has obtained an object, the focus of the document. The activeElement. TagName / / tagName tag name

Example: turn off the input method when determining the body to get the cursor.


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; //Id and name are more common
alert(" controls "+testval+" Get focus ");
}
</script>
<input name="a" type="text" value=" test a" onclick="test();">
<input name="b" type="text" value=" test b" onclick="test();">


Related articles: