当前位置: 移动技术网 > IT编程>开发语言>JavaScript > 用图片替换checkbox原始样式并实现同样的功能

用图片替换checkbox原始样式并实现同样的功能

2019年06月02日  | 移动技术网IT编程  | 我要评论
1.结构: <div class="box1">   <input/>   <div class="box2">    

1.结构:

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

2.css里:

  1)设置图片的div绝对定位,调整让它与原始input重合。

  2)设置input的宽高与图片的div一样大,这样精确一点。

  3)然后设置input:position:relative;再设置它的z-index,让它浮在图片那个div的上面。

  4)调整两个的位置,让之重合。

  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背景变为没选中,而第二个radio背景变为被选中,还用上面的jq里 的代码是不能实现的,会出现第一个选中之后,再点第二个会让两个的背景都是被选中的图片。

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

$(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的样式以达到目的,结果无终而返。因为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>

总结

以上所述是小编给大家介绍的用图片替换checkbox原始样式并实现同样的功能 ,希望对大家有所帮助

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网