Sự kiện tiêu điểm được kích hoạt khi một phần tử được hoặc mất tiêu điểm. Sau đây là các focusevents -
| Sự kiện | Mô tả |
|---|---|
| Làm mờ | Sự kiện này được kích hoạt khi một phần tử mất tiêu điểm. |
| Tiêu điểm | Sự kiện này được kích hoạt khi phần tử được lấy tiêu điểm. |
| focusin | Sự kiện này được kích hoạt khi phần tử sắp lấy tiêu điểm. |
| ocusout | Sự kiện này được kích hoạt khi một phần tử sắp mất tiêu điểm. |
Sau đây là mã cho các sự kiện tiêu điểm trong JavaScript -
Ví dụ
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
body {
font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
}
input {
padding: 10px;
}
</style>
</head>
<body>
<h1>Focus events in Javascript</h1>
<form id="form">
<input type="text" placeholder="Username" />
<input class="passW" type="password" placeholder="Password" />
</form>
<h3>Focus on the above password input box to change its background color</h3>
<script>
let passEle = document.querySelector(".passW");
passEle.addEventListener("focus", () => {
passEle.style.backgroundColor = "#90EE90";
});
</script>
</body>
</html> Đầu ra
Tập trung vào trường mật khẩu -