欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

用圖片替換checkbox原始樣式并實現(xiàn)同樣的功能

 更新時間:2018年11月15日 09:28:47   作者:站住,別跑  
這篇文章主要介紹了用圖片替換checkbox原始樣式并實現(xiàn)同樣的功能 ,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友參考下吧

1.結(jié)構(gòu):

<div class="box1">
  <input/>
  <div class="box2">
    <img />
  </div>
</div>

2.css里:

  1)設置圖片的div絕對定位,調(diào)整讓它與原始input重合。

  2)設置input的寬高與圖片的div一樣大,這樣精確一點。

  3)然后設置input:position:relative;再設置它的z-index,讓它浮在圖片那個div的上面。

  4)調(diào)整兩個的位置,讓之重合。

  5)設置input:opacity:0;

3.jq里:

$(function(){
    $("input").click(function(){
      if(this.checked){
        $(this).siblings(".box2").find("img").attr("src","被選中的圖片的src");
      }else{
        $(this).siblings(".box2").find("img").attr("src","未被選中的圖片的src");
      }
    })
  })

注意:siblings()里面只能有一個屬性。不能siblings(".box2>img"),這樣是錯的。

  radio修改默認樣式也是同樣的道理,但是在jq里就要改一些東西了,

  如果想點擊第一個radio之后,再點擊同一個name的radio,它被選中之后,第一個radio背景變?yōu)闆]選中,而第二個radio背景變?yōu)楸贿x中,還用上面的jq里 的代碼是不能實現(xiàn)的,會出現(xiàn)第一個選中之后,再點第二個會讓兩個的背景都是被選中的圖片。

  所以要在判定是否被選中的時候,加上:

$(function(){
    $("input").click(function(){
      $("input").each(function(){
         if(this.checked){
          $(this).siblings(".box2").find("img").attr("src","被選中的圖片的src");
         }else{
          $(this).siblings(".box2").find("img").attr("src","未被選中的圖片的src");
         }
      })
   })
})

  需要遍歷一下input

補充:

用圖片替換checkbox的樣式 

  由于項目的需要,需要在登錄的時候保存用戶名,就需要使用checkbox。其中的checkbox樣式為給定的一張圖片,非選中:

  ,選中:

開始準備改變checkbox的樣式以達到目的,結(jié)果無終而返。因為checkbox的大小,樣式很難改變,反正打不到我想要的效果。于是試圖通過用圖片替換checkbox的樣式。

主要知識點:

 (1)通過 label 元素內(nèi)點擊圖片,就會觸發(fā)checkbox控件。就是說,當用戶選擇該標簽時,瀏覽器就會自動將焦點轉(zhuǎn)到和標簽相關(guān)的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>

總結(jié)

以上所述是小編給大家介紹的用圖片替換checkbox原始樣式并實現(xiàn)同樣的功能 ,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評論