JS基础--输入框获取焦点

来源:互联网 发布:图片处理器软件 编辑:程序博客网 时间:2024/06/18 01:10
<div>我是第一种方式:<label for="taobao" id="first">我是第一种方式</label><input type="text" id="taobao"><br>我是第二种方式:<label for="JD" id="second">我是第二种方式</label><input type="text" id="JD"><br>我是:<input type="text" placeholder="占位符"><br>账号:<label for="JD" id="third">京东</label><input type="text" id="zhagnhao"><br></div>

var taobao=document.getElementById('taobao');var first=document.getElementById('first');var second=document.getElementById('second');var jingdong=document.getElementById('JD');var zhagnhao=document.getElementById('zhagnhao');taobao.onfocus=function(){    first.style.display="none";}    taobao.onblur=function(){    if(taobao.value===""){    first.style.display="block";    }}jingdong.oninput=function(){if(this.value===""){second.style.display="block"}else{             second.style.display="none";}}

注意:

使用行内样式时,eg:onclick="fn()",当onclick事件中使用this时,此时的this指的是window,而不是标签本身。
所以在行内使用事件时,要传递参数,eg:onclick="fn(this)",此时的this指的是真正的标签本身

0 0
原创粉丝点击