首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何通过选择all id使js代码更简单

如何通过选择all id使js代码更简单
EN

Stack Overflow用户
提问于 2022-04-14 06:39:27
回答 2查看 38关注 0票数 1

如何使下面的代码更简单、更简短

html:

代码语言:javascript
复制
<div class="navigation-manual">
    <label id="point-1" for="radio1" class="manual-btn other" onclick="myFunction1()">1</label>
    <label id="point-2" for="radio2" class="manual-btn" onclick="myFunction2()">2</label>
    <label id="point-3" for="radio3" class="manual-btn" onclick="myFunction3()">3</label>
    <label id="point-4" for="radio4" class="manual-btn" onclick="myFunction4()">4</label>
  </div>

我制作了4个按钮,如果单击按钮1,背景将变为白色,当单击按钮2时,背景将变为白色,但按钮1的背景色恢复到原来的颜色,也适用于其他按钮。

联署材料:

代码语言:javascript
复制
function myFunction1(){
    document.getElementById("point-1").style.background = "white";
    document.getElementById("point-2").style.background = "none";
    document.getElementById("point-3").style.background = "none";
    document.getElementById("point-4").style.background = "none";
  }

  function myFunction2(){
    document.getElementById("point-2").style.background = "white";
    document.getElementById("point-1").style.background = "none";
    document.getElementById("point-3").style.background = "none";
    document.getElementById("point-4").style.background = "none";
  }

  function myFunction3(){
    document.getElementById("point-3").style.background = "white";
    document.getElementById("point-1").style.background = "none";
    document.getElementById("point-2").style.background = "none";
    document.getElementById("point-4").style.background = "none";
  }

  function myFunction4(){
    document.getElementById("point-4").style.background = "white";
    document.getElementById("point-1").style.background = "none";
    document.getElementById("point-2").style.background = "none";
    document.getElementById("point-3").style.background = "none";
  }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-04-14 07:00:25

您可以尝试首先重置所有项目的背景。

代码语言:javascript
复制
function labelClick(selectedId){
    document.querySelectorAll('.manual-btn').forEach(label => {
      label.style.background = "none";
    })
    document.getElementById(selectedId).style.background = "white";
  }
代码语言:javascript
复制
.navigation-manual {
  background-color: gray;
}
代码语言:javascript
复制
<div class="navigation-manual">
    <label id="point-1" for="radio1" class="manual-btn other" onclick="labelClick('point-1')">1</label>
    <label id="point-2" for="radio2" class="manual-btn" onclick="labelClick('point-2')">2</label>
    <label id="point-3" for="radio3" class="manual-btn" onclick="labelClick('point-3')">3</label>
    <label id="point-4" for="radio4" class="manual-btn" onclick="labelClick('point-4')">4</label>
  </div>

或者,您可以尝试侦听现有的单选按钮更改事件。

票数 0
EN

Stack Overflow用户

发布于 2022-04-14 06:54:55

这个应该可以用白色代替红色

代码语言:javascript
复制
const labels = document.querySelectorAll(".manual-btn")

labels.forEach(l => l.addEventListener("click", () => {
  labels.forEach(l => {l.style.background = "none"})
  l.style.background = "red"
}));
代码语言:javascript
复制
<div class="navigation-manual">
    <label id="point-1" for="radio1" class="manual-btn other" >1</label>
    <label id="point-2" for="radio2" class="manual-btn" >2</label>
    <label id="point-3" for="radio3" class="manual-btn" >3</label>
    <label id="point-4" for="radio4" class="manual-btn">4</label>
  </div>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71867434

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档