焦点事件
Tutorial: DOM操作
Category: JS
Published: 2026-04-07 13:58:26
Views: 20
Likes: 0
Comments: 0
<!DOCTYPE html>
<html lang="en">
<head>
<style>
input {
color: #999;
}
</style>
</head>
<body>
<input type="text" value="手机" />
<script>
var text = document.querySelector("input");
text.onfocus = function () {
if (this.value === "手机") {
this.value = "";
}
this.style.color = "#333";
};
text.onblur = function () {
if (this.value === "") {
this.value = "手机";
}
this.style.color = "#999";
};
</script>
</body>
</html>