<style>
input {
color: gray;
outline: none;
}
</style>
</head>
<body>
<input type="text" value="请输入内容" id="txt">
<script src="learn2.js"></script>
<script>
//注册获取焦点的事件
my$("txt").onfocus = function () {
//判断文本框是否为默认的内容
if (this.value = "请输入内容") {
//清空文本框
this.value = "";
//重新设置字体颜色
this.style.color = "black";
}
};
//注册失去焦点的事件
my$("txt").onblur = function () {
//判断文本框是否有内容
if (this.value.length == 0) {
this.value = "请输入内容";
this.style.color = "gray";
}
};
</script>
</body>
获取焦点失去焦点事件
最新推荐文章于 2024-05-18 15:28:02 发布