JS determines that an object has gained focus document.activeElement.tagName //tagName tag name
Example: Determine whether to close the input method when the body obtains the cursor.
Copy the code code 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; //id and name are more commonly used and should
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();">