程序问答   发布时间:2022-05-31  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了修复选中后的单选按钮(Survey Scale CSS)背景颜色,并且所有按钮都没有被选中大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决修复选中后的单选按钮(Survey Scale CSS)背景颜色,并且所有按钮都没有被选中?

开发过程中遇到修复选中后的单选按钮(Survey Scale CSS)背景颜色,并且所有按钮都没有被选中的问题如何解决?下面主要结合日常开发的经验,给出你关于修复选中后的单选按钮(Survey Scale CSS)背景颜色,并且所有按钮都没有被选中的解决方法建议,希望对你解决修复选中后的单选按钮(Survey Scale CSS)背景颜色,并且所有按钮都没有被选中有所启发或帮助;

单选按钮背景颜色在悬停和检查时正确设置。但是在检查后修复颜色以及其他小于所选颜色的问题。

/*main raTing class*/

.raTing {
  position: inherit;
  display: inline-flex;
  wIDth: 100%;
  Box-sizing: border-Box;
  border: 1px solID #000;
  BACkground: linear-gradIEnt( to left,#f00,#ff0,#0f0);
}

.raTing input {
  display: none;
}

.raTing label {
  position: inherit;
  margin: inherit;
  display: flex;
  cursor: pointer;
  wIDth: 100%;
  justify-content: center;
  align-items: center;
  Transition: 0.5s;
  BACkground: #fff;
  color: #000;
  border-right: 1px solID #000;
}

.raTing:not(:checked)>label:hover,.raTing:not(:checked)>label:hover~label {
  BACkground: transparent;
  cursor: pointer;
}

.raTing>input:checked+label:hover,.raTing>label:hover~input:checked~label,.raTing>input:checked~label:hover~label {
  BACkground: transparent;
  cursor: pointer;
}
<div>
  <label for="rdbraTing1">*1. XYZ </label>

  <!-- 1st Radio button List -->

  <div class="raTing" ID="rdbraTing1">
    <input type="radio" value="7" ID="7">
    <label for="7">7</label>
    <input type="radio" value="6" ID="6">
    <label for="6">6</label>
    <input type="radio" value="5" ID="5">
    <label for="5">5</label>
    <input type="radio" value="4" ID="4">
    <label for="4">4</label>
    <input type="radio" value="3" ID="3">
    <label for="3">3</label>
    <input type="radio" value="2" ID="2">
    <label for="2">2</label>
    <input type="radio" value="1" ID="1">
    <label for="1">1</label>
    <input type="radio" value="NA" ID="NA">
    <label for="NA">NA</label>
  </div>
</div>

<div>
  <label for="rdbraTing2">*2. ABC </label>

  <!-- 2nd Radio button List -->

  <div class="raTing" ID="rdbraTing2">
    <input type="radio" value="7" ID="7">
    <label for="7">7</label>
    <input type="radio" value="6" ID="6">
    <label for="6">6</label>
    <input type="radio" value="5" ID="5">
    <label for="5">5</label>
    <input type="radio" value="4" ID="4">
    <label for="4">4</label>
    <input type="radio" value="3" ID="3">
    <label for="3">3</label>
    <input type="radio" value="2" ID="2">
    <label for="2">2</label>
    <input type="radio" value="1" ID="1">
    <label for="1">1</label>
    <input type="radio" value="NA" ID="NA">
    <label for="NA">NA</label>
  </div>
  <div>

解决方法

您可以为此使用 vanilla javascript...

很确定您希望选择元素行上被点击的元素之后的所有元素。如果我错了,请告诉我,我会调整答案。

使用从所选元素检查 nodeList 中的下一个元素的函数,您可以将该函数嵌入到 forEach 循环中并事件监听器

使用 querySELEctorAll() 获取要迭代的元素节点列表。然后使用 forEach() 迭代。传递参数以获取迭代值 val,然后对该迭代值运行 eventListener。通过侦听器 e 传递事件对象,并使用 target 属性定位被点击的元素。

然后使用 e.target 将一个类应用到将对其应用样式的目标元素。

然后我们想将样式应用到 nodeList 中剩余的 LABEL 类型的兄弟节点。这里我们使用了一个辅助函数 nextUntil() 来获取 nodeList 中剩余的元素从被点击事件监听器触发的选中元素到指定的目标元素在节点列表中。 注意: 我为每个 raTing 子节点添加了一个空的 span 标记以供选择。

我们使用 e.target 并运行 for/in 循环来获取我们想要应用 class 的目标元素。这是通过在 for 循环中使用条件来实现的,该条件检查元素的类型 nodename 是否为 LABEL。现在我们有了从开始到目标结束的标签,我们将类应用到选定的 nodeList

let raTing = document.querySELEctorAll('.raTing label');

let nextUntil = (elem,SELEctor,filter) => {
  // Setup siblings array
  let siblings = [];
  // Get the next sibling element
  elem = elem.nextElementSibling;
  // As long as a sibling exists
  while (elem) {
    // If we've reached our match,bail
    if (elem.matches(SELEctor)) break;
    // If filtering by a SELEctor,check if the sibling matches
    if (filter && !elem.matches(filter)) {
      elem = elem.nextElementSibling;
      conTinue;
    }
    // Otherwise,push it to the siblings array
    siblings.push(elem);
    // Get the next sibling element
    elem = elem.nextElementSibling;
  }
  return siblings;
};

// pass the parameters into the function 
let changeBg = (el,eType,target,className) => {
  // loop over the elements 
  el.forEach((val) => {
    // add an event listener to the current element and pass the event object
    val.addEventListener(eType,(E) => {
      // lets reset the targeted parent elements childNodes class name      
      // if it is set to the class name that sets the BG transparency
      // if user clicks to change the setTing they have already set
      for (let i = 0; i < e.target.parentNode.childNodes.length; i++) {
        if (e.target.parentNode.childNodes[i].className === className) {
          e.target.parentNode.childNodes[i].classList.remove(className);          
        }
      }
      // use the event target to target the current element being triggered
      e.target.className = classname;
      // target the last element to stop the Helper function and pass in the e.target
      let next = nextUntil(e.target,'span');
      // iterate over the remaining elements
      for (let i in next) {
        // Use the index on the values for targetTing and apply the class that will style it 
        if (next[i].nodename === target) {
          next[i].className = classname;
        }
      }
    })
  })
}

// call function and pass in the unique parameters
// iteraTing over the raTing elements
// adding a click listener
// targeTing the "LABEL" elements in the nodeList
// adding the class 'rated'
changeBg(raTing,'click','LABEL','rated');
/*main raTing class*/

.raTing {
  position: inherit;
  display: inline-flex;
  width: 100%;
  box-sizing: border-box;
  border: 1px solid #000;
  BACkground: linear-gradient( to left,#f00,#ff0,#0f0);
}

.raTing input {
  display: none;
}

.raTing label {
  position: inherit;
  margin: inherit;
  display: flex;
  cursor: pointer;
  width: 100%;
  justify-content: center;
  align-items: center;
  transition: 0.5s;
  BACkground: #fff;
  color: #000;
  border-right: 1px solid #000;
}

.rated {
  BACkground: transparent !important;
}

.raTing:not(:checked)>label:hover,.raTing:not(:checked)>label:hover~label {
  BACkground: transparent;
  cursor: pointer;
}

.raTing>input:checked+label:hover,.raTing>label:hover~input:checked~label,.raTing>input:checked~label:hover~label {
  BACkground: transparent;
  cursor: pointer;
}
<div>
  <label for="rdbRaTing1">*1. XYZ </label>

  <!-- 1st Radio button list -->

  <div class="raTing" id="rdbRaTing1">
    <input type="radio" value="7" id="7">
    <label for="7">7</label>
    <input type="radio" value="6" id="6">
    <label for="6">6</label>
    <input type="radio" value="5" id="5">
    <label for="5">5</label>
    <input type="radio" value="4" id="4">
    <label for="4">4</label>
    <input type="radio" value="3" id="3">
    <label for="3">3</label>
    <input type="radio" value="2" id="2">
    <label for="2">2</label>
    <input type="radio" value="1" id="1">
    <label for="1">1</label>
    <input type="radio" value="NA" id="NA">
    <label for="NA">NA</label>
    <span></span>
  </div>
</div>

<div>
  <label for="rdbRaTing2">*2. ABC </label>

  <!-- 2nd Radio button list -->

  <div class="raTing" id="rdbRaTing2">
    <input type="radio" value="7" id="7">
    <label for="7">7</label>
    <input type="radio" value="6" id="6">
    <label for="6">6</label>
    <input type="radio" value="5" id="5">
    <label for="5">5</label>
    <input type="radio" value="4" id="4">
    <label for="4">4</label>
    <input type="radio" value="3" id="3">
    <label for="3">3</label>
    <input type="radio" value="2" id="2">
    <label for="2">2</label>
    <input type="radio" value="1" id="1">
    <label for="1">1</label>
    <input type="radio" value="NA" id="NA">
    <label for="NA">NA</label>
    <span></span>
  </div>
  <div>

大佬总结

以上是大佬教程为你收集整理的修复选中后的单选按钮(Survey Scale CSS)背景颜色,并且所有按钮都没有被选中全部内容,希望文章能够帮你解决修复选中后的单选按钮(Survey Scale CSS)背景颜色,并且所有按钮都没有被选中所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: