可以使用JavaScript代码在HTML中判断某个控件是否获得了焦点。
可以通过检查该元素的 "document.activeElement" 属性来判断。如果该属性与要检查的元素相同,则表示该元素获得了焦点。
例如:
if (document.activeElement=== document.getElementById("myInput")) {
console.log("input is focused");
}
在上面的代码中,我们通过 "document.getElementById("myInput")" 获取了 id 为 "myInput" 的元