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)
   { = 'disabled';
function test(){
var srcElem = document.activeElement
var testval =; //Id and name are more common
alert(" controls "+testval+" Get focus ");
<input name="a" type="text" value=" test a" onclick="test();">
<input name="b" type="text" value=" test b" onclick="test();">

Related articles: