2018年6月27日 更新
找到最為簡單的僅僅使用css3的方案

1 <!DOCTYPE html> 2 <html lang="en"> 3 4 <head> 5 <meta charset="UTF-8"> 6 <title>Document</title> 7 <style type="text/css"> 8 input[type="radio"]+label::before { 9 content: ""; 10 /*不換行空格*/ 11 display: inline-block; 12 vertical-align: middle; 13 font-size: 18px; 14 width: 10px; 15 height: 10px; 16 margin-right: 10px; 17 border-radius: 50%; 18 border: 2px solid #01cd78; 19 text-indent: 15px; 20 line-height: 1; 21 padding: 4px; 22 } 23 24 input[type="radio"]:checked+label::before { 25 background-color: #01cd78; 26 background-clip: content-box; 27 } 28 29 input[type="radio"] { 30 position: absolute; 31 clip: rect(0, 0, 0, 0); 32 } 33 </style> 34 </head> 35 36 <body> 37 <div class="female"> 38 <input type="radio" id="female" name="sex" checked="" /> 39 <label for="female">女</label> 40 </div> 41 <div class="male"> 42 <input type="radio" id="male" name="sex" /> 43 <label for="male">男</label> 44 </div> 45 </body> 46 47 </html>
在最近的一次開發中,或者在之前的開發中,經常性的用到單選框這個form表單元素。而ui給出的設計方案絕對也不是原生的radio樣式,面對這種場景,自定義radio效果成為一種解決方案。
直接上圖,如下
測試代碼,如下
1 <!DOCTYPE html> 2 <html lang="en"> 3 4 <head> 5 <meta charset="UTF-8"> 6 <title>自定義radio和checkbox</title> 7 <style type="text/css"> 8 #ceshi label input { 9 display: none; 10 } 11 12 #ceshi label span { 13 display: inline-block; 14 width: 18px; 15 height: 18px; 16 border-radius: 50%; 17 border: 2px solid #ddd; 18 box-sizing: border-box; 19 position: relative; 20 top: 3px; 21 margin-right: 6px; 22 } 23 24 #ceshi label span.active { 25 border: 3px solid red; 26 } 27 </style> 28 </head> 29 30 <body> 31 <form id="ceshi" action="test.php" method="get"> 32 33 <label> 34 <span></span> 35 <input type="radio" name="t" value="這是測試1">這是測試1 36 </label> 37 <label> 38 <span></span> 39 <input type="radio" name="t" value="這是測試2">這是測試2 40 </label> 41 <label> 42 <span></span> 43 <input type="radio" name="t" value="這是測試3">這是測試3 44 </label> 45 <input type="submit" name="" value="提交"> 46 </form> 47 <script type="text/javascript"> 48 49 Object.prototype.siblings = function() { 50 var arr = []; //保存兄弟節點 51 var prev = this.previousSibling; //o的前一個同胞節點 52 //先往上查詢兄弟節點 53 while (prev) { 54 if (prev.nodeType == 1 && prev.tagName == this.tagName) { 55 arr.unshift(prev); //數組首部插入數組,保證節點順序 56 } 57 prev = prev.previousSibling; //把上一節點賦值給prev 58 } 59 //往下查詢兄弟節點 60 var next = this.nextSibling; //o的后一個同胞節點 61 while (next) { 62 if (next.nodeType == 1 && next.tagName == this.tagName) { 63 arr.push(next); //數組尾部插入,保證節點順序 64 } 65 next = next.nextSibling; //下一節點賦值給next,用於循環 66 } 67 return arr; 68 } 69 //判斷HTMLElement是否含有某個class 70 Object.prototype.hasClass = function(cls) { 71 return this.className.match(new RegExp('(\\s|^)' + cls + '(\\s|$)')); 72 } 73 //HTMLElement對象添加類 74 Object.prototype.addClass = function(cls) { 75 if (!this.hasClass(cls)) this.className += " " + cls; 76 } 77 //HTMLElement對象刪除類 78 Object.prototype.removeClass = function(cls) { 79 if (this.hasClass(cls)) { 80 var reg = new RegExp('(\\s|^)' + cls + '(\\s|$)'); 81 this.className = this.className.replace(reg, ' '); 82 } 83 } 84 85 function nativeSelfRadio(dom) { 86 dom.getElementsByTagName("span")[0].addClass("active"); 87 dom.siblings().forEach(function(ele, val) { 88 ele.getElementsByTagName("span")[0].removeClass('active'); 89 //ele.getElementsByTagName("span")[0].classList.remove('active'); 90 }) 91 } 92 //綁定事件 93 var len=document.getElementById("ceshi").getElementsByTagName("label"); 94 for (var i = 0; i < len.length; i++) { 95 len[i].getElementsByTagName("input")[0].checked=false;//設置不選中 96 len[i].onclick=function(){ 97 nativeSelfRadio(this); 98 } 99 } 100 101 </script> 102 </body> 103 104 </html>
最初開發時候,也習慣了用jquery,但慢慢也意識到原生不熟走不遠的道理,於是開始各種原生實現。上述測試代碼均采用原生js實現;
本人覺得需要關注的地方有:
1)、函數掛載的原型對象是HTMLElement,實際原型對象寫為Object也是可以的
2)、添加或者刪除類可以自己來寫,也可以用HTML5的接口classList,添加或者刪除類
3)、避免返回該頁面,radio依然為選中狀態,需要加載完頁面后將radio選中狀態設置為false,如果業務需要單獨選中哪個,就需要定制了