思路:点击输入框时,高亮显示当前输入框,当然不能在输入框后面写onFocus和onBlur

过程:经过研究资料,发现用getElementsByTagName可以起到控制所有的Input的作用,这样就可以通过className改变样式了。

改进:第一次发布时候,只做了简单增加class和删除class动作,使用后发现程序员的input原来都有自己的cssClass,这样无法还原原来样式。第二次发布使用cStyle全局变量保存当前的样式,以便在失去焦点以后重新还原,使用后发现覆盖原来样式以后,无法保证原来设定的一些属性,如width等,所以使用追加样式,问题是追加的样式无法覆盖掉原来的已经定义过的样式,最后使用!important解决。

目前测试满足系统当前的需求。

相关代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Focus</title>
<script type="text/javascript">
window.onload=function(){
    
     var inputs=document.getElementsByTagName("input");
     var cStyle
    
    
   for(var i=0; i<inputs.length; i++){
                if(inputs[i].type=="text"){

     inputs[i].onfocus= function(){cStyle=this.className;  this.className=cStyle+" fStyle"; };
     // alert(cStyle)
     inputs[i].onblur= function(){this.className=cStyle ;};
    
    }
            }
     }

</script>
<style type="text/css">
<!--
input {
 border: 1px solid #d0d0d0;
 background-color: #efefef;
 font-family:Verdana, Geneva, sans-serif;
 font-size:11px;
 line-height:12px;
 color: #000;
 text-decoration: none;
 
}

.fStyle {
 background-color: #fefefe!important;
 border:1px solid #666666;
}
.bg1 {
 background-color: #0F6;
}
.bg2 {
 background-color: #F30;
}
-->
</style>
</head>

<body>

<input type="text" name="textfield" id="textfield"  class="bg1"/>
<input type="text" name="textfield2" id="textfield2"  class="bg2"/>
<input type="text" name="textfield3" id="textfield3"  disabled="disabled"/>
<input name="textfield4" type="text" id="textfield4" value="200.00" readonly="readonly"/>
<input type="button" name="button" id="button" value="Submit" />


</body>
</html>

posted on 2010-03-18 14:08  NeoCong  阅读(364)  评论(0编辑  收藏  举报