【问题标题】:onclick radio button active color change using css使用 css 的 onclick 单选按钮活动颜色更改
【发布时间】:2021-06-25 08:08:10
【问题描述】:

当我们单击单选按钮时,背景颜色和标签颜色会发生变化,我已经编写了 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>

【问题讨论】:

  • 如果你想保持这种 HTML 结构,你不能单独使用 CSS。 CSS 可以选择向下和向右的 DOM 树,而不是向上或向左选择。

标签: javascript html css css-transitions


【解决方案1】:

请检查一下,如果这能解决您的问题,我已删除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

【讨论】:

  • 在这里,当我单击任何一个开关时,同一页面上有多个开关,然后其他开关也会发生变化。
  • 好的,我就是按照你的代码,能不能提供个codepen,我看看
  • 这正是我想要的父母case-inner-info 我添加了更多类.pink 所以在这个粉红色类开关bg 中将是粉红色并且对于不同的颜色是相同的。我试过这样if (e.target.className === 'pink') { e.target.closest('div.case-switch').querySelectorAll('input').forEach(i =&gt; i.parentNode.style.backgroundColor = i.dataset.ref === e.target.dataset.ref ? 'pink' : 'transparent'); },但它不起作用。根据父类开关颜色变化,我有 3 类粉色、蓝色、绿色
  • 它在小提琴中?我可以看看
【解决方案2】:

使用标签的位置更改开关选择的位置。 但这不是最好的解决方案。您可以在 switch-label-on 和 switch-label-off 类上使用背景,并像使用 css 的示例一样对其进行样式设置。

我的两分钱。

【讨论】:

    【解决方案3】:

    // 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>

    【讨论】:

      猜你喜欢
      • 2021-09-08
      • 1970-01-01
      • 2014-09-04
      • 2015-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-03
      相关资源
      最近更新 更多