当前位置: 移动技术网 > IT编程>开发语言>JavaScript > 原生javascript自定义input[type=radio]效果示例

原生javascript自定义input[type=radio]效果示例

2019年09月07日  | 移动技术网IT编程  | 我要评论

本文实例讲述了原生javascript自定义input[type=radio]效果。分享给大家供大家参考,具体如下:

找到最为简单的仅仅使用css3的方案

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>document</title>
  <style type="text/css">
  input[type="radio"]+label::before {
    content: "";
    /*不换行空格*/
    display: inline-block;
    vertical-align: middle;
    font-size: 18px;
    width: 10px;
    height: 10px;
    margin-right: 10px;
    border-radius: 50%;
    border: 2px solid #01cd78;
    text-indent: 15px;
    line-height: 1;
    padding: 4px;
  }
  input[type="radio"]:checked+label::before {
    background-color: #01cd78;
    background-clip: content-box;
  }
  input[type="radio"] {
    position: absolute;
    clip: rect(0, 0, 0, 0);
  }
  </style>
</head>
<body>
  <div class="female">
    <input type="radio" id="female" name="sex" checked="" />
    <label for="female">女</label>
  </div>
  <div class="male">
    <input type="radio" id="male" name="sex" />
    <label for="male">男</label>
  </div>
</body>
</html>

在最近的一次开发中,或者在之前的开发中,经常性的用到单选框这个form表单元素。而ui给出的设计方案绝对也不是原生的radio样式,面对这种场景,自定义radio效果成为一种解决方案。

直接上图,如下

测试代码,如下

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>自定义radio和checkbox</title>
  <style type="text/css">
  #ceshi label input {
    display: none;
  }
  #ceshi label span {
    display: inline-block;
    width: 18px;
    height: 18px;
    border-radius: 50%;
    border: 2px solid #ddd;
    box-sizing: border-box;
    position: relative;
    top: 3px;
    margin-right: 6px;
  }
  #ceshi label span.active {
    border: 3px solid red;
  }
  </style>
</head>
<body>
  <form id="ceshi" action="test.php" method="get">
    <label>
      <span></span>
      <input type="radio" name="t" value="这是测试1">这是测试1
    </label>
    <label>
      <span></span>
      <input type="radio" name="t" value="这是测试2">这是测试2
    </label>
    <label>
      <span></span>
      <input type="radio" name="t" value="这是测试3">这是测试3
    </label>
    <input type="submit" name="" value="提交">
  </form>
  <script type="text/javascript">
    object.prototype.siblings = function() {
      var arr = []; //保存兄弟节点 
      var prev = this.previoussibling; //o的前一个同胞节点 
      //先往上查询兄弟节点 
      while (prev) {
        if (prev.nodetype == 1 && prev.tagname == this.tagname) {
          arr.unshift(prev); //数组首部插入数组,保证节点顺序 
        }
        prev = prev.previoussibling; //把上一节点赋值给prev 
      }
      //往下查询兄弟节点 
      var next = this.nextsibling; //o的后一个同胞节点 
      while (next) {
        if (next.nodetype == 1 && next.tagname == this.tagname) {
          arr.push(next); //数组尾部插入,保证节点顺序 
        }
        next = next.nextsibling; //下一节点赋值给next,用于循环 
      }
      return arr;
    }
    //判断htmlelement是否含有某个class
    object.prototype.hasclass = function(cls) {
      return this.classname.match(new regexp('(\\s|^)' + cls + '(\\s|$)'));
    }
    //htmlelement对象添加类
    object.prototype.addclass = function(cls) {
      if (!this.hasclass(cls)) this.classname += " " + cls;
    }
    //htmlelement对象删除类
    object.prototype.removeclass = function(cls) {
      if (this.hasclass(cls)) {
        var reg = new regexp('(\\s|^)' + cls + '(\\s|$)');
        this.classname = this.classname.replace(reg, ' ');
      }
    }
    function nativeselfradio(dom) {
      dom.getelementsbytagname("span")[0].addclass("active");
      dom.siblings().foreach(function(ele, val) {
        ele.getelementsbytagname("span")[0].removeclass('active');
        //ele.getelementsbytagname("span")[0].classlist.remove('active');
      })
    }
    //绑定事件
    var len=document.getelementbyid("ceshi").getelementsbytagname("label");
    for (var i = 0; i < len.length; i++) {
      len[i].getelementsbytagname("input")[0].checked=false;//设置不选中
      len[i].onclick=function(){
         nativeselfradio(this);
      }
    }
  </script>
</body>
</html>

最初开发时候,也习惯了用jquery,但慢慢也意识到原生不熟走不远的道理,于是开始各种原生实现。上述测试代码均采用原生js实现;

本人觉得需要关注的地方有:

1)、函数挂载的原型对象是htmlelement,实际原型对象写为object也是可以的

2)、添加或者删除类可以自己来写,也可以用html5的接口classlist,添加或者删除类

3)、避免返回该页面,radio依然为选中状态,需要加载完页面后将radio选中状态设置为false,如果业务需要单独选中哪个,就需要定制了

感兴趣的朋友可以使用在线html/css/javascript代码运行工具:测试上述代码运行效果。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网