js-onfocus事件; js-onblur事件 ;

js-onfocus事件:onfocus 事件在对象获得焦点时发生;

该时间支持的html标签

<a>, <acronym>, <address>, <area>, <b>, <bdo>, <big>, <blockquote>, <button>, 

<caption>, <cite>, <dd>, <del>, <dfn>, <div>, <dl>, <dt>, <em>, <fieldset>, 

<form>, <frame>, <frameset>, <h1> to <h6>, <hr>, <i>, <iframe>, <img>, <input>, 

<ins>, <kbd>, <label>, <legend>, <li>, <object>, <ol>, <p>, <pre>, <q>, 

<samp>, <select>, <small>, <span>, <strong>, <sub>, <sup>, <table>, <tbody>, 

<td>, <textarea>, <tfoot>, <th>, <thead>, <tr>, <tt>, <ul>, <var>

支持该事件的js对象

button, checkbox, fileUpload, layer, frame, password, radio, reset, select, submit, 

text, textarea, window

实例

<html>

<head>

<script type="text/javascript">

function setStyle(x)

{

document.getElementById(x).style.background="yellow"

}

</script>

</head>



<body>



First name: <input type="text"

onfocus="setStyle(this.id)" id="fname" />

<br />

Last name: <input type="text"

onfocus="setStyle(this.id)" id="lname" />



</body>

</html>

当用鼠标点击input窗口的时候 input背景颜色为黄色

---------------------------------------------------------------------------------------------------------------------------------------------------------------

js-onblur事件:onblur 事件会在对象失去焦点时发生;

该时间支持的html标签 与onfocus一样

支持该事件的js对象 与onfocus一样

实例

<html>

<head>

<script type="text/javascript">

function upperCase()

{

var x=document.getElementById("fname").value

document.getElementById("fname").value=x.toUpperCase()    //toUpperCase() 方法用于把字符串转换为大写

}

</script>
</head>
<body> 
输入您的姓名:
<input type="text" id="fname" onblur="upperCase()"/>
</body>
</html>

 在input窗口中输入小写的英文 当input失去焦点的时候 将刚输入的input的value内容转成大写

----------------------------------------------以下是自己写的一个小例子-----------------------------------------------------------------------------------

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

 <head>

  <title> New Document </title>

  <meta charset="UTF-8">

  <meta name="Generator" content="EditPlus">

  <meta name="Author" content="">

  <meta name="Keywords" content="">

  <meta name="Description" content="">

  <link rel="stylesheet" type="text/css" href="">

  <style type="text/css">

    .aaa{width:155px;height:23px;overflow:hidden;}

    #inputone{position:absolute;background:transparent;outline:none;}

    .spanone{margin-left:50px;line-height:25px;}

  </style>

 </head>



 <body>

 <div class="aaa" id="aaa">

   <input id="inputone" type="text" value=""  autocomplete="off">

   <span id="spanone" class="spanone">点击</span>

 </div>

 </body>

 <script type="text/javascript">

 <!--

    var inputone=document.getElementById('inputone');

    var spanone=document.getElementById('spanone');

    var diva=document.getElementById('aaa');

    inputone.onfocus=function(){

        spanone.style.display="none";

    }

    inputone.onblur=function(){

        if (inputone.value.length==0)

        {

            spanone.style.display="block";

        }

    }



 //-->

 </script>

</html>

 

input上有个'点击'的显示 当鼠标点击input的时候,'点击'文字消失 js判断 如果input的vlaue值为空 鼠标移开 也就是input失去焦点的时候 ‘点击’文字再次出现

 注释:background:transparent; 背景透明

 

 

你可能感兴趣的:(focus)