在 HTML 中,当元素获得焦点时执行脚本?
htmlweb developmentfront end technology更新于 2026/1/17 16:44:17
使用 onfocus 属性,当元素获得焦点时执行脚本。
示例
您可以尝试运行以下代码来实现 onfocus 属性 −
<!DOCTYPE html>
<html>
<body>
<p>Enter subject name below,</p>
Subject: <input type = "text" id = "sname" onfocus = "display(this.id)">
<br>
<script>
function display(val) {
document.getElementById(val).style.background = "blue";
}
</script>
</body>
</html>
相关文章
有用资源
html 参考教程 - 该教程包含有关 html 的更多信息:https://www.cainiaomax.com/html/

