需求:京東的獲取焦點
思路:京東的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>