当元素在 HTML 中获得焦点时执行一个脚本?
在 HTML 中使用 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>
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP