JS之文本框获得焦点和失去焦点

需求:京东的获取焦点
思路:京东的input按钮获取了插入条光标立刻删除内容。失去插入条光标显示文字
获取事件源和相关元素

<body>
京东:<input type="text" name="jd" id="inp1" value="我是京东" /><br>
淘宝:<label for="inp2">我是淘宝</label><input type="text" name="tb" id="inp2" /><br>
placeholder:<input type="text" placeholder="我是placeholder"/>

<script>
//需求:京东的获取焦点
//思路:京东的input按钮获取了插入条光标立刻删除内容。失去插入条光标显示文字
//获取事件源和相关元素
var inp1 = document.getElementById("inp1");
//绑定事件
inp1.onfocus =function(){

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

</script>
原文地址:https://www.cnblogs.com/creazybeauty/p/8057420.html