09
2019
07

js文本框input获取焦点和失去焦点

//需求:用户的获取焦点

//思路:用户的input按钮获取了插入条光标立刻删除内容。失去插入条光标显示文字

//获取事件源和相关元素

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
	</head>
	<body>
		用户:<input type="text" name="jd" id="inp1" value="我是用户" /><br>
昵称:<label for="inp2">我是昵称</label><input type="text" name="tb" id="inp2"/><br>
密码:<input type="text" placeholder="我是密码"/>
	</body>
	<script type="text/javascript">
var inp1 = document.getElementById("inp1");
//绑定事件
inp1.onfocus =function(){

//书写驱动程序
if (this.value=="我是用户") {
    this.value = "";
}
}
inp1.onblur = function(){
if (this.value==="") {
    this.value = "我是用户";
}
}

</script>
	</script>
</html>

原文链接:https://www.qiquanji.com/post/8210.html

本站声明:网站内容来源于网络,如有侵权,请联系我们,我们将及时处理。

微信扫码关注

更新实时通知

« 上一篇 下一篇 »

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。