用圖片替換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>

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM