表单美化-原生javascript和jQuery单选按钮(兼容IE6)


最近很多人问怎么美化表单的元素,大家都知道表单元素在各个浏览器中的表现不一,反正也是特别的丑,那么问题就来了,我们能自己设计表单元素的外观么?答案是可以的,现在我们就来试试吧。我们用两种方式来实现这一效果,一种是用原生的javascript 和一种是现在特别流行的javascript框架jQuery来实现这一效果。

首先我们先来用javascript的方式,不了解原生javascript的童鞋可以跳过这个直接进去jQuery的部分。javascript是前端的基础建议大家还是要学习下原生javascript。

我们的想法是:利用单选按钮是否被选中和是否不给选择的特性来为按钮的父元素添加对应的样式,就是说用什么的样式是由按钮的状态来决定。

1:原生javascript美化单选按钮 (代码测试我用了IE5+,现代浏览器就不用说了是可以的,就IE有8以前有点奇葩。)

我们采取的是把原来的表单元素的透明度改为0,或者直接display:none掉然后添加标签配合脚本模拟点击效果。

下面直接给出demo 童鞋们直接复制就好

  1. <!DOCTYPE HTML>  
  2. <html lang="en-US">  
  3. <head>  
  4. <meta charset="UTF-8">  
  5. <title></title>  
  6. </head>  
  7. <style type="text/css">  
  8. .coin{background: url(radio.png) no-repeat -90px 0;width: 20px;height: 20px;display: inline-block;position: absolute;left: 0;top: 0;}   /* 然后我们自己定义按钮图片 */  
  9. label{position: relative;padding-left: 10px;margin-left: 20px;}  
  10. label input{filter: progid:DXImageTransform.Microsoft.Alpha(opacity=0);opacity: 0;}  /* 把原来的按钮透明度改为0 使它隐藏不见 */  
  11. .checked .coin{background-position: -60px -30px;}  
  12.   
  13. </style>  
  14. <script type="text/javascript">  
  15. function show(index){  /*定义一个函数*/  
  16. var tyle=document.getElementById("tyle");  /*获取ID为tyle的元素*/  
  17. var label=tyle.getElementsByTagName("label"); /*通过ID为tyle的元素获取下面的所有label元素,注意了返回的是包含所有label元素的数组*/  
  18. // 我们要访问每个label元素那么就要遍历  
  19. for(var i=0;i<label.length;i++){  
  20. if(i==index){  /*判断当前的i和传进来的参数是否相等 */  
  21. label[i].className="checked";/*相等的话 就给类*/  
  22. }else{label[i].className=null;}  /*不是的话 类为空*/  
  23. }  
  24. }  
  25. </script>  
  26. <body>  
  27. <div id="tyle">  
  28. <form action="#">  
  29. <label for="man" class="checked"  onclick="show(0)">  <!-- 当这个label元素被点击的时候调用show()这个函数并且传参0 以便函数确认点击谁 -->  
  30. <span class="coin"></span>  
  31. <input type="radio" id="man" value="man" name="radtype" checked>男  
  32. </label>  
  33. <label for="woman" onclick="show(1)">  
  34. <span class="coin"></span>  
  35. <input type="radio"  id="woman" value="woman" name="radtype">女  
  36.   </label>  
  37.    <label for="man-woman" onclick="show(2)">  
  38. <span class="coin"></span>  
  39. <input type="radio" id="man-woman" value="man-woman" name="radtype">  
  40. 不男不女  
  41.    </lable>  
  42. </form>  
  43. </div>  
  44. </body>  
  45. </html>  



用到的图片

效果


现在我们来完善下代码,增加不能点击功能

  1. <!DOCTYPE HTML>  
  2. <html lang="en-US">  
  3. <head>  
  4. <meta charset="UTF-8">  
  5. <title></title>  
  6. </head>  
  7. <style type="text/css">  
  8. .coin{background: url(radio.png) no-repeat -90px 0;width: 20px;height: 20px;display: inline-block;position: absolute;left: 0;top: 0;}   /* 然后我们自己定义按钮图片 */  
  9. label{position: relative;padding-left: 10px;margin-left: 20px;}  
  10. label input{filter: progid:DXImageTransform.Microsoft.Alpha(opacity=0);opacity: 0;}  /* 把原来的按钮透明度改为0 使它隐藏不见 */  
  11. .checked .coin{background-position: -60px -30px;}  
  12. .disabled  .coin{background-position: 0 -90px;}  
  13.   
  14. </style>  
  15. <script type="text/javascript">  
  16. function show(index){  /*定义一个函数*/  
  17. var tyle=document.getElementById("tyle");  /*获取ID为tyle的元素*/  
  18. var label=tyle.getElementsByTagName("label"); /*通过ID为tyle的元素获取下面的所有label元素,注意了返回的是包含所有label元素的数组*/  
  19. // 我们要访问每个label元素那么就要遍历  
  20. for(var i=0;i<label.length;i++){  
  21. if(i==index){  /*判断当前的i和传进来的参数是否相等 */  
  22. label[i].className="checked";/*相等的话 就给类*/  
  23. }else{  
  24. if(label[i].className=="disabled"){ // 再判断一下当前的label是否有disabled这个类  
  25. label[i].className="disabled"  //如果有的话 就保持这个类  
  26. }else{label[i].className=null;} //否则就清空类  
  27. }  /*不是的话 类为空*/  
  28. }  
  29. }  
  30. </script>  
  31. <body>  
  32. <div id="tyle">  
  33. <form action="#">  
  34. <label for="man" class="checked"  onclick="show(0)">  <!-- 当这个label元素被点击的时候调用show()这个函数并且传参0 以便函数确认点击谁 -->  
  35. <span class="coin"></span>  
  36. <input type="radio" id="man" value="man" name="radtype" checked>男  
  37. </label>  
  38. <label for="woman" onclick="show(1)">  
  39. <span class="coin"></span>  
  40. <input type="radio"  id="woman" value="woman" name="radtype">女  
  41.   </label>  
  42.    <label for="man-woman" class="disabled"> //增加一个不能点击的元素  
  43. <span class="coin"></span>  
  44. <input type="radio" id="man-woman"  disabled value="man-woman" name="radtype">  
  45. 不男不女  
  46.    </lable>  
  47. </form>  
  48. </div>  
  49. </body>  
  50. </html>  



效果


2:jQuery美化单选按钮 (代码测试我用了IE5+,现代浏览器就不用说了是可以的,就IE有8以前有点奇葩。)

用jQuery做的代码简洁了许多,有木有。。。。效果和上面一致


  1. <!DOCTYPE HTML>  
  2. <html lang="en-US">  
  3. <head>  
  4. <meta charset="UTF-8">  
  5. <title></title>  
  6. </head>  
  7. <style type="text/css">  
  8. .coin{background: url(radio.png) no-repeat -90px 0;width: 20px;height: 20px;display: inline-block;position: absolute;left: 0;top: 0;}   /* 然后我们自己定义按钮图片 */  
  9. label{position: relative;padding-left: 10px;margin-left: 20px;}  
  10. label input{filter: progid:DXImageTransform.Microsoft.Alpha(opacity=0);opacity: 0;}  /* 把原来的按钮透明度改为0 使它隐藏不见 */  
  11. .checked .coin{background-position: -60px -30px;}  
  12. .disabled  .coin{background-position: 0 -90px;}   
  13. </style>  
  14. <script  src="jquery-1.11.1.min.js"></script>  
  15. <script type="text/javascript">  
  16. $(function(){  
  17. $("#tyle label").click(function() {  /*为ID为tyle里面的所有label绑定点击事件*/  
  18. $(this).not('.disabled').addClass('checked').siblings().not('.disabled').removeClass('checked');  
  19. /*当前被点击的label并且里面没有类disabled的就增加类checked和它的前后兄弟关系的label并且不含有类disabled的label都移除类checked*/  
  20. });  
  21. })  
  22. </script>  
  23. <body>  
  24. <div id="tyle">  
  25. <form action="#">  
  26. <label for="man" class="checked" >   
  27. <span class="coin"></span>  
  28. <input type="radio" id="man" value="man" name="radtype" checked>男  
  29. </label>  
  30. <label for="woman">  
  31. <span class="coin"></span>  
  32. <input type="radio"  id="woman" value="woman" name="radtype">女  
  33.   </label>  
  34.    <label for="man-woman" class="disabled">  
  35. <span class="coin"></span>  
  36. <input type="radio" id="man-woman"  disabled value="man-woman" name="radtype">  
  37. 不男不女  
  38.    </lable>  
  39. </form>  
  40. </div>  
  41. </body>  
  42. </html>  


来吧亲,既然用到了JQ那么我们现在就来简化下代码,是它可以更好的重用吧。

  1. <!DOCTYPE HTML>  
  2. <html lang="en-US">  
  3. <head>  
  4. <meta charset="UTF-8">  
  5. <title></title>  
  6. </head>  
  7. <style type="text/css">  
  8. .coin{background: url(radio.png) no-repeat -90px 0;width: 20px;height: 20px;display: inline-block;position: absolute;left: 0;top: 0;}   /* 然后我们自己定义按钮图片 */  
  9. label{position: relative;padding-left: 10px;margin-left: 20px;}  
  10. label input{filter: progid:DXImageTransform.Microsoft.Alpha(opacity=0);opacity: 0;}  /* 把原来的按钮透明度改为0 使它隐藏不见 */  
  11. .checked .coin{background-position: -60px -30px;}  
  12. .disabled  .coin{background-position: 0 -90px;}   
  13. </style>  
  14. <script  src="jquery-1.11.1.min.js"></script>  
  15. <script type="text/javascript">  
  16. function showRadio(){    
  17. if($(".radioList").length){ /*每次执行函数showRadio()的时候判断下时候有.radioList(长度不为0即有.radioList)*/  
  18. $(".radioList").each( function() {  /*.radioList每次执行方法 showRadio()都把每个.radioList遍历下每个.radioList都执行.checked移除*/  
  19. $(this).removeClass('checked')  
  20. });  
  21. $(".radioList input[type='radio']:checked").each( function() {   /*每次执行showRadio()都把选中的按钮删选出来并把所有选出来的按钮的父label标签增加.checked*/  
  22. $(this).parent('label').addClass('checked');  
  23. });  
  24. $(".radioList input[type='radio']:disabled").each( function() { /*每次执行showRadio()都把不能选择的按钮删选出来并把所有选出来的按钮的父label标签增加.disabled*/  
  25. $(this).parent('label').addClass('disabled');  
  26. });  
  27. }  
  28. }  
  29. $(function(){  
  30. $(".radioList").click(function() {  /*任何一个.radioList被点击的时候执行 showRadio()*/  
  31. showRadio();  
  32. });  
  33. showRadio(); /*页面载入的时候执行showRadio()*/  
  34. })  
  35.   
  36.   
  37. </script>  
  38. <body>  
  39. <form action="#">  
  40. <label for="man" class="radioList">   
  41. <span class="coin"></span>  
  42. <input type="radio" id="man" value="man" name="radtype" checked="checked">男  
  43. </label>  
  44. <label for="woman" class="radioList">  
  45. <span class="coin"></span>  
  46. <input type="radio"  id="woman" value="woman" name="radtype">女  
  47. </label>  
  48. <label for="man-woman" class="radioList">  
  49. <span class="coin" class="radioList"></span>  
  50. <input type="radio" id="man-woman"  disabled="disabled" value="man-woman" name="radtype">  
  51. 不男不女  
  52. </lable>  
  53. </form>  
  54.   
  55. </body>  
  56. </html>  


现在我们把代码做最后的一次简化最终效果如下

  1. <!DOCTYPE HTML>  
  2. <html lang="en-US">  
  3. <head>  
  4. <meta charset="UTF-8">  
  5. <title></title>  
  6. </head>  
  7. <style type="text/css">  
  8. .radioList .coin{background: url(radio.png) no-repeat -90px 0;width: 20px;height: 20px;display: inline-block;position: absolute;left: 0;top: 0;}   /* 然后我们自己定义按钮图片 */  
  9. .radioList{position: relative;padding-left: 10px;margin-left: 20px;}  
  10. .radioList input{filter: progid:DXImageTransform.Microsoft.Alpha(opacity=0);opacity: 0;}  /* 把原来的按钮透明度改为0 使它隐藏不见 */  
  11. .radio_checked .coin{background-position: -60px -30px;}  
  12. .radio_disabled  .coin{background-position: 0 -90px;}   
  13. </style>  
  14. <script  src="jquery-1.11.1.min.js"></script>  
  15. <script type="text/javascript">  
  16. var radio_parent=".radioList";  
  17. var radio_input=radio_parent+" input[type='radio']";  
  18. var checked_radio_css="radio_checked";  
  19. var disabled_radio_css="radio_disabled";  
  20. function showRadio(){    
  21. if($(radio_parent).length){ /*每次执行函数showRadio()的时候判断下时候有.radioList(长度不为0即有.radioList)则执行里面的代码*/  
  22. $(radio_parent).each( function() {  /*第一步每个.radioList里的样式(.checked)移除一下*/  
  23. $(this).removeClass(checked_radio_css)  
  24. });  
  25. $(radio_input+":checked").each( function() {   /*第二步把.radioList里选中的按钮筛选出来并把所有选出来的按钮的父label标签增加.checked*/  
  26. $(this).parent(radio_parent).addClass(checked_radio_css);  
  27. });  
  28. $(radio_input+":disabled").each( function() { /*第三步把radioList里不能选择的按钮删选出来并把所有选出来的按钮的父label标签增加.disabled*/  
  29. $(this).parent(radio_parent).addClass(disabled_radio_css);  
  30. });  
  31. }  
  32. }  
  33. $(function(){  
  34. $(radio_parent).click(function() {  /*任何一个.radioList被点击的时候执行 showRadio()*/  
  35. showRadio();  
  36. });  
  37. showRadio(); /*页面载入的时候执行showRadio()*/  
  38. })  
  39.   
  40.   
  41. </script>  
  42. <body>  
  43. <form action="#">  
  44. <label for="man" class="radioList">   
  45. <span class="coin"></span>  
  46. <input type="radio" id="man" value="man" name="radtype" checked="checked">男  
  47. </label>  
  48. <label for="woman" class="radioList">  
  49. <span class="coin"></span>  
  50. <input type="radio"  id="woman" value="woman" name="radtype">女  
  51. </label>  
  52. <label for="man-woman" class="radioList">  
  53. <span class="coin" class="radioList"></span>  
  54. <input type="radio" id="man-woman"  disabled="disabled" value="man-woman" name="radtype">  
  55. 不男不女  
  56. </lable>  
  57. </form>  
  58.   
  59.   
  60. </body>  
  61. </html>  



代码下载

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值