JQuery统一复写美化项目中所有radio单选按钮样式
2020-12-12 20:35
1671 查看
老项目要升级改版,对于分散在各页面的样式不好处理,怕有遗漏,尤其是优化input表单,修改其默认样式,接下来,我将给大家分享一下,我在项目中的总结。
本文为Echoyya、所创,转载请带上原文链接,感谢
https://www.cnblogs.com/echoyya/p/14126709.html
效果
上代码:
1.简单搞一搞 CSS,此处代码有折叠,Click Me~.custom-radio { float: left; } .custom-radio input { position: absolute; left: -9999px; vertical-align: middle; } .custom-radio label { cursor: pointer; padding-right: 20px; line-height: 30px; padding-left: 25px; position: relative; display: inline-block; } .custom-radio label:hover { color: #FF6200; } .custom-radio label::after { content: ''; display: block; position: absolute; top: 6px; left: 0; width: 16px; height: 16px; outline: 0; border: 1px solid #D5D5D5; border-radius: 50%; } .custom-radio label.checked::after { border: 6px solid #FF6200; width: 6px; height: 6px; } .custom-radio label, .custom-radio label.checked { border: none; background: none; }
2.简单搞一搞 HTML
<input type="radio" name="yesOrNo" id="yes" checked /> <label for="yes">是</label> <input type="radio" name="yesOrNo" id="no" /> <label for="no">否</label>
3.开整 ~~~~
首先分析一下实现思路:
-
定义一个JQuery的扩展方法,页面加载完毕,input radio循环调用
-
创建一个新的Div,并设置类名,用于定义css
-
使用输入的ID得到相关的标签,将input radio及对应的id的label,放进上述Div中
-
绑定自定义事件,触发它,绑定点击,焦点等事件
<script src="./jquery-1.11.1.min.js"></script> <script> $.fn.customInput = function () { return $(this).each(function () { if ($(this).is('[type=radio]')) { var input = $(this); var label = $('label[for=' + input.attr('id') + ']'); label.add(input).wrapAll('<div class="custom-' + input.attr('type') + '"></div>'); label.hover = function () { $(this).addClass('hover'); }; input.bind('updateState', function () { 56c input.is(':checked') ? label.addClass('checked') : label.removeClass('checked'); }) .click(function () { $('input[name=' + $(this).attr('name') + ']').trigger('updateState'); }) .focus(function () { // 自定义处理逻辑 label.addClass('focus'); if (input.is(':checked')) $(this).addClass('checkedFocus'); }) .blur(function () { // 自定义处理逻辑 label.removeClass('focus checkedFocus'); }); } }); }; $('input:radio').each(function () { var $this = $(this); $this.customInput(); //初始化单选框 }); </script>
相关文章推荐
- jQuery获取单选按钮radio选中值与去除所有radio选中状态的方法
- jQuery获取单选按钮radio选中值与去除所有radio选中状态
- jQuery:1.5.4.3,表格变色(单击行,把当行的单选按钮(radio)设为选中状态,并应用当前样式)
- jQuery设置单选按钮radio选中/不可用的实例
- 微信小程序 修改(自定义) 单选/复选按钮样式 checkbox/radio样式自定义
- jquery操作radio(单选按钮):动态选中、取值
- Jquery操作radio单选按钮
- jQuery获取单选按钮(Radio)当前选中项的值
- jQuery设置单选按钮radio选中/不可用的实例代码
- jquery判断单选按钮radio是否选中的方法
- 表单样式美化--复选框和单选按钮
- jquery判断单选按钮radio是否选中的方法
- jQuery如何获取选中单选按钮radio的值
- 【jQuery】:radio单选按钮选择器
- css input[type=file] input[type=radio] input[type=checkbox]样式美化,input上传按钮美化
- 用 jquery 操作 radio单选按钮
- 单选按钮radio样式的改变
- jquery判断单选按钮radio是否选中的方法
- 改变单选按钮radio的默认样式
- jQuery 实现单选按钮(radio)勾选和取消,使用prop()代替attr() 踩坑博客