自定义修改input=radio的样式(选中+禁用)

本文介绍如何在移动端开发中自定义input=radio的样式,通过隐藏原始的radio按钮并使用label元素来实现符合UI设计要求的样式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

自定义修改input=radio的样式

前言

鉴于在移动端开发中用到的移动端的组件库内没有符合UI设计要求的radio样式,只能使用最原始的input来造成符合需求的框

实现

样式与原始的差不多,只是选中的颜色改成绿色

		<input
        id="inputChoice1"
         v-model="advice"
         class="select"
         type="radio"
         :disabled="!isLeader"
         name="leaderadvice"
         value="choice1"
       />
       <label for="inputChoice1"></label>
       <span>choice1</span>
       <input
         id="inputChoice2"
         v-model="advice"
         class="select"
         type="radio"
         :disabled="!isLeader"
         name="leaderadvice"
         value="choice2"
       />
       <label for="inputChoice2"></label>
       <span>choice2</span>

选中时:
在这里插入图片描述
禁用时
在这里插入图片描述
样式的修改主要还是借助各种选择期,以及将选中框的样式寄托在label上

样式代码如下

//将input的圆框隐藏
 input[type='radio'] {
    width: 0.32rem;
    height: 0.32rem;
    display: none;
  }
//定位label到圆框的位置,并修改样式为圆框
  label {
    position: relative;
    top: 0.1rem;
    width: 0.32rem;
    height: 0.32rem;
    border-radius: 50%;
    border: 1px solid #999;//默认为边框
  }

  /*设置选中的radio的样式*/
  /* + 是兄弟选择器,获取选中后的label元素*/
  input:checked + label {
    background: url('../../../assets/images/mobile/icon-check.png'); //直接借助背景图片
    border: none;
  }
   /*设置禁用的radio的样式*/
  input:checked[disabled] + label {
    background: url('../../../assets/images/mobile/icon-check-disabled.png');
    border: none;
  }
<body> <div style="width: 485px"> <h1 style="height: 40px; padding: 10px; font-size: 30px; text-align: center">待办事项列表</h1> <div style=" padding: 10px"> <div style="height: 32px; padding: 10px 0; font-size: 14px"> <input style="width: 400px" placeholder="请输入新增内容" /> <button>新增</button> </div> <div> <input type="checkbox" value="" /><span>全选</span> <span style="margin: 0 50px"> <input type="radio" value="" name="radio" /><span style="margin-right: 4px">全部</span> <input type="radio" value="" name="radio" /><span style="margin-right: 4px">已选中</span> <input type="radio" value="" name="radio" /><span>未选中</span> </span> <button>删除已选中</button> </div> <ul> <li> <input type="checkbox" value="" /> <span style="color: red">待办事项1</span> <span class="button-group"> <button>编辑</button> <button>删除</button> </span> </li> <li> <input type="checkbox" value="" /> <span style="color: green">待办事项2</span> <span class="button-group"> <button>编辑</button> <button>删除</button> </span> </li> <li> <input type="checkbox" value="" /> <span style="color: blue">待办事项3</span> <span class="button-group"> <button>编辑</button> <button>删除</button> </span> </li> </ul> </div> <!-- <div>©2024 启明星计划前端授课组</div> --> <div class="copyright" style="height: 40px; padding: 10px; text-align: center; font-size: 16px"> ©2025 启明星计划前端授课组 </div> </div> </body>1、"待办事项列表" 页面内容居中显示 2、列表 hover 时有背景样式 3、初始状态下默认选中“代办事项1” 4、表单增加label标签的使用 5、 使用js禁用“删除”按钮 6、实现勾选事项checkbox,文本出现中划线,编辑和删除按钮隐藏 7、使用js操作dom,将底部“前端授课组”文字为超链接,地址自定义,在新标签中打开
最新发布
07-29
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值