用图片替换checkbox的样式

 

       由于项目的需要,需要在登录的时候保存用户名,就需要使用checkbox。其中的checkbox样式为给定的一张图片,非选中:

,选中:

开始准备改变checkbox的样式以达到目的,结果无终而返。因为checkbox的大小,样式很难改变,反正打不到我想要的效果。于是试图通过用图片替换checkbox的样式。

主要知识点:

 (1)通过 label 元素内点击图片,就会触发checkbox控件。就是说,当用户选择该标签时,浏览器就会自动将焦点转到和标签相关的checkbox控件上。

<input type="checkbox"  class="clsCheckBox" ID="chkRememberPwd"/>    

<label for="chkRememberPwd"><img src="${ctx}/images/more/selector_default.png" width="38" height="38" onclick="checkbox()" /></label>

 //checkbox通过css设置为隐藏

.clsCheckBox{  
    display:none;  
  }

(2)点击图片时通过JS进行图片的切换。代码如下:

  var result = true;
   function checkbox() {
     if (result==true)
   {
    document.images[0].src = "${ctx}/images/more/selector_focus.png";
 result=false;
   }
   else if(result==false)
   {
   document.images[0].src = "${ctx}/images/more/selector_default.png"
   result=true;
   }
   }

登录页面代码:

 

<!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" xmlns="http://www.w3.org/1999/html">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>用户登录</title>

<script>history.forward();</script>
<script type="text/javascript">
  function func_key(event){
        if (event.keyCode == 13){
               doLogin();
        }
    }

 function cleanup(o){
   o.value = "";
   o.style.textAlign="left";
   }

  var result = true;
   function checkbox() {
     if (result==true)
   {
    document.images[0].src = "${ctx}/images/more/selector_focus.png";
 result=false;
   }
   else if(result==false)
   {
   document.images[0].src = "${ctx}/images/more/selector_default.png"
   result=true;
   }
   }
</script>
<style type="text/css">

.clsCheckBox{  
    display:none;  
}
</style>
</head>

<body >
<div id="header">
<div  id="headInfo"><span>登录</span></div>
</div>
<form>
<table class="partb" cellspacing="0" cellpadding="0">
     <tr class="partba">
     <td class="partbb">用户名</td>
     <td><input name="userClientNumber" id="userClientNumber"  class="partbc" type="text" value="" onfocus="cleanup(this)"/></td>
     </tr>
     <tr class="partbe"></tr>  
     <tr class="partba">
     <td class="partbb">密码</td>
     <td><input name="userPassword" id="userPassword" class="partbc" type="password"  value="" onfocus="cleanup(this)" /></td>
     </tr>
     </table>
<table style="width:450px;margin:-20px 15px;padding:0px;" width="480" border="0">
  <tr><td><input type="checkbox"  class="clsCheckBox" ID="chkRememberPwd"/>

<label for="chkRememberPwd"><img src="${ctx}/images/more/selector_default.png" width="38" height="38" onclick="checkbox()" /></label>

<span style="padding-left:15px;font-size:24px;color:#999999;vertical-align:top;line-height:38px;">记住用户名</span></td>
    <td style="text-align:right;padding-right:0px;"><a  href="modifyPassword!load.action"  style="border-bottom: 1px solid #ffa200;"><span style="color:#ffa200;font-size:24px;">找回登录密码</span></a></td>
  </tr>
</table>
<table class="partb" style="margin-top:180px;" border="0">
  <tr>
    <td width="240" align="left" ><div class="bottondiv" ><input  class="inputbotton" type="button"  onclick="javascript:doLogin();" value="登 录"/></div></td>
    <td width="240" align="left"><div class="bottondiv" ><input class="inputbotton" type="button" onclick="location='register!loadRegister.action'" value="注 册"/></div></td>
  </tr>
</table>
</form>
</body>
</html>

 

 

posted @ 2012-08-24 16:36  学无先后 达者为先  阅读(8266)  评论(0编辑  收藏  举报