onclick 单选按钮活动颜色更改使用 css

onclick radio button active color change using css

当我们点击单选按钮时,背景颜色和标签颜色会改变 我已经写了 CSS 但它不起作用。我不想只用我的代码更改我的 HTML 结构,我怎样才能在活动标签上添加颜色。谁能推荐我一下。

const handleVisibility = (e) => {
  e.target.closest('.case-inner-info').querySelectorAll('.inner_div [data-ref]').forEach(i => i.style.display = i.dataset.ref === e.target.dataset.ref ? 'block' : 'none');
}
const allOnOffButtons = document.querySelectorAll("div.case-inner-info input");
allOnOffButtons.forEach(button => {
  button.addEventListener('click', handleVisibility);
})
.switch-label {
  position: relative;
  width: 58px;
  float: left;
  line-height: 26px;
  font-size: 16px;
  font-weight: 900;
  text-align: center;
  cursor: pointer;
  color: #051326;
  z-index: 2;
}

.switch-input {
  display: none;
}

.switch-input:checked .switch-label {
  color: #fff;
  -webkit-transition: 0.15s ease-out;
  -o-transition: 0.15s ease-out;
  transition: 0.15s ease-out;
  -webkit-transition-property: color;
  -o-transition-property: color;
  transition-property: color;
}

.switch-input:checked+.switch-label-off~.switch-selection {
  left: 60px;
}

.switch-selection {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  position: absolute;
  left: 0;
  width: 70px;
  height: 40px;
  border-radius: 4px;
  color: #fff;
  z-index: 1;
  -webkit-transition: left 0.15s ease-out;
  -o-transition: left 0.15s ease-out;
  transition: left 0.15s ease-out;
}

.case-switch .switch-selection {
  background-color: red;
}
<div class="case-inner-info">
  <div class="case-switch-wrap">
    <div class="inner_div">
      <div class="case-info" data-ref="on">
        <p> Paragrapgh1</p>
      </div>
      <div class="case-code-wrap" style="display: none;" data-ref="off">
        <p> Paragrapgh2</p>
      </div>
    </div>
    <div class="case-switch">
      <label class="switch-label switch-label-on">
      <input type="radio" class="switch-input" name="switch-toggle" data-ref="on" checked>
      On</label>
      <label class="switch-label switch-label-off">
      <input type="radio" class="switch-input" name="switch-toggle" data-ref="off">
      Off</label>
      <span class="switch-selection"></span>
    </div>
  </div>
</div>

使用标签的位置更改开关选择的位置。 但这不是最好的解决方案。您可以只在 switch-label-on 和 switch-label-off 类 上使用背景,并使用 css.

像您的示例一样设置样式

我的两分钱。

请检查一下,如果这能解决您的问题,我已经删除了 span

<div class="case-inner-info">
  <div class="case-switch-wrap">
    <div class="inner_div">
      <div class="case-info" data-ref="on">
        <p> Paragrapgh1</p>
      </div>
      <div class="case-code-wrap" style="display: none;" data-ref="off">
        <p> Paragrapgh2</p>
      </div>
    </div>
    <div class="case-switch">
      <label class="switch-label switch-label-on">
      <input type="radio" class="switch-input" name="switch-toggle" data-ref="on" checked>
      On</label>
      <label class="switch-label switch-label-off">
      <input type="radio" class="switch-input" name="switch-toggle" data-ref="off">
      Off</label>
      
    </div>
  </div>
</div>
const allOnOffButtons = document.querySelectorAll("div.case-inner-info input");
const handleVisibility = (e) => {
  
  e.target.closest('.case-inner-info').querySelectorAll('.inner_div [data-ref]')
  .forEach(i => 
     i.style.display = i.dataset.ref === e.target.dataset.ref ? 'block' : 'none');
     allOnOffButtons.forEach(i => i.parentNode.style.backgroundColor = i.dataset.ref === e.target.dataset.ref ? 'red' : 'transparent');
}

allOnOffButtons.forEach(button => {
  button.addEventListener('click', handleVisibility);
})

这是codepen

// Cache the inputs
const inputs = document.querySelectorAll('.switch-input');

// Add event listeners to them
inputs.forEach(() => addEventListener('change', handleRadio, false));

function handleRadio(e) {

  // Remove the switch-selection class from
  // all the inputs/label background
  inputs.forEach(label => {
    label.parentNode.classList.toggle('switch-selection');
  });

  // And then just toggle it on the radio that's
 // been clicked
  e.target.classList.toggle('switch-selection');
}
.switch-selection { background-color: red; }
<div class="case-switch">
  <label class="switch-label switch-selection switch-label-on">
      <input type="radio" class="switch-input" name="switch-toggle" data-ref="on" checked>
      On
  </label>
  <label class="switch-label switch-label-off">
      <input type="radio" class="switch-input" name="switch-toggle" data-ref="off">
      Off
    </label>
</div>