javascript - 检查单选后如何为输入单选按钮及其标签添加相同的边框?

标签 javascript html css bootstrap-4 frontend

HTML:

<div class="form-check fs-3 mb-3">
  <input id="first_question" name="first_question" type="radio" value="n">
  <label for="first_question">no</label>
</div>

选中单选后如何为输入单选按钮及其标签添加相同的边框?

我都尝试过:

.form-check:checked{
border:2px solid red;
border-radius: 4px;
color:red;
}

和:

.form-check::after{
border:2px solid red;
border-radius: 4px;
color:red;
}

但是他们都不起作用。有 friend 可以帮忙吗?

最佳答案

似乎 :has() 选择器在许多浏览器中不起作用。这是codepen demo在不同的浏览器中尝试一下。

.form-check:has(> #first_question:checked){
  display: inline-flex;
  padding: 10px 50px;
  border: 2px solid red;
  border-radius: 30px;
}

#first_question:checked:after {
    content: '';
    display: inline-block;
    width: 10px;
    height: 10px;
    border: 5px solid red;
    border-radius: 50%;
    top: -4px;
    left: -4px;
    position: relative;
    background-color: white;    
}

#first_question:checked + label{
  margin-left: 5px;
}
<div class="form-check fs-3 mb-3">
  <input id="first_question" name="first_question" type="radio" value="n">
  <label for="first_question">Excellent</label>
</div>

关于javascript - 检查单选后如何为输入单选按钮及其标签添加相同的边框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76766387/

相关文章:

javascript - 为什么 PhantomJS 不抓取它重定向到的页面?

javascript - 如果表单字段是默认的,则将类添加到按钮?

javascript - 需要制作一个 Twitter 分享按钮来分享来自 JS 函数的随机引用

jquery - 在窗口调整大小时自动调整右侧容器的宽度

javascript - || 是什么意思?意思是?

javascript - MooTools 补间口吃/打嗝

jquery - Joomla 3 + Bootstrap 3 - 单击时折叠的导航栏跳跃

html - 如何将div内容设置为垂直?

javascript - Angular Material 中 Mat-Menu 的自定义样式背景颜色

javascript - 使用 jquery 函数播放或暂停视频