更改所有点击按钮的颜色

发布于 2025-01-22 03:34:08 字数 632 浏览 2 评论 0原文

我在以下形式的HTML页面中有一组按钮:

<button
      id="testID"
      mat-mini-fab
      ngClass="list-button"
      (click)="onClick($event)"
    >
      Press
    </button>

我尝试更改属于.list-button类的每个按钮的颜色,然后使用以下CSS代码单击它:

.list-button:focus {
  background-color: #7d698d;
}

但是,当按钮i的颜色i的颜色i单击每次更改 (所有先前单击的按钮的颜色也会更改为原始颜色)。 我该如何解决?我希望所有单击的按钮保持其新颜色。

我还尝试为此类的按钮分配一个ID,并在onClick()方法中更改其颜色,但没有成功。同样的问题仍然存在。你能帮我吗?

  onclick(event: any) {
    const btn = document.getElementById('testID');
    if (btn) btn.style.backgroundColor = '#7d698d';
  }

I have a set of buttons in an html page of the following form:

<button
      id="testID"
      mat-mini-fab
      ngClass="list-button"
      (click)="onClick($event)"
    >
      Press
    </button>

I try to change the color of each button belonging to .list-button class after clicking on it using the following css code:

.list-button:focus {
  background-color: #7d698d;
}

However, while the color of the button I click each time changes
(the color of all the previously clicked buttons also changes back to their original color).
How could I fix it? I want all the clicked buttons to remain their new color.

I also tried assigning an id to the buttons of this class and changing their color inside the onClick() method as follows without success. The same problem remains. Could you help me, please?

  onclick(event: any) {
    const btn = document.getElementById('testID');
    if (btn) btn.style.backgroundColor = '#7d698d';
  }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

盗琴音 2025-01-29 03:34:08
  • id必须是唯一的,
  • 您可以使用该函数并通过el,它将this作为参数函数函数onclick(el){.............. 并使用它onclick(this)
  • 无需获得id您可以直接使用el.styleevent .target.Style请参阅下一个示例
function onClick(el){
  el.style.backgroundColor = '#7d698d';
}
<button
  id="testID-1"
  mat-mini-fab
  ngClass="list-button"
  onclick="onClick(this)"
>
  Press
</button>

<button
  id="testID-2"
  mat-mini-fab
  ngClass="list-button"
  onclick="onClick(this)"
>
  Press
</button>

另外您可以使用该功能并通过event等于event作为参数函数函数onclick(event){..... 。}并使用 onclick(event)

function onClick(event){
  event.target.style.backgroundColor = '#7d698d';
}
<button
  id="testID-1"
  mat-mini-fab
  ngClass="list-button"
  onclick="onClick(event)"
>
  Press
</button>

<button
  id="testID-2"
  mat-mini-fab
  ngClass="list-button"
  onclick="onClick(event)"
>
  Press
</button>

  • ID must be unique
  • You can use the function and pass el which will refer to this as an argument function onClick(el){......} and use it onClick(this)
  • No need to get the id you can directly use el.style or event.target.style see the next examples

function onClick(el){
  el.style.backgroundColor = '#7d698d';
}
<button
  id="testID-1"
  mat-mini-fab
  ngClass="list-button"
  onclick="onClick(this)"
>
  Press
</button>

<button
  id="testID-2"
  mat-mini-fab
  ngClass="list-button"
  onclick="onClick(this)"
>
  Press
</button>

Also You can use the function and pass event which equal event as an argument function onClick(event){......} and use it onClick(event)

function onClick(event){
  event.target.style.backgroundColor = '#7d698d';
}
<button
  id="testID-1"
  mat-mini-fab
  ngClass="list-button"
  onclick="onClick(event)"
>
  Press
</button>

<button
  id="testID-2"
  mat-mini-fab
  ngClass="list-button"
  onclick="onClick(event)"
>
  Press
</button>

忆依然 2025-01-29 03:34:08

您需要获取发射事件的按钮的ID

onclick(event: any) {
  const btn = document.getElementById(event.target.id);
  if (btn) btn.style.backgroundColor = '#7d698d';
}

You need to get the id of the button that fired the event

onclick(event: any) {
  const btn = document.getElementById(event.target.id);
  if (btn) btn.style.backgroundColor = '#7d698d';
}
萌吟 2025-01-29 03:34:08

单击另一个按钮后,先前单击按钮的颜色重置的颜色是将焦点设置在最后一个单击按钮上的原因。尝试在Venkatesh答案中使用代码。

The reason why the color of the previously clicked buttons is resetting after you click on another one is that the focus is set on the last clicked button. Try using the code in the Venkatesh's answer.

浪荡不羁 2025-01-29 03:34:08

无需JS。在样式表中使用focus选择器。

:focus 允许在接受键盘事件或其他用户输入的元素上进行选择。

w3

button.list-button:focus {
  background-color: #7d698d;
  color: white;
}
<button class="list-button">Click</button>

用JS编辑〜。此方法使用.querySelectorAll(“ button”)并每次单击按钮(s)时切换样式的类.focus。有多个按钮时,这很好。

// Get all the elements on the basis of query selector (button)
let btn = document.querySelectorAll("button");

for (var i = 0; i < btn.length; i++) {
  (function(index) {
    btn[index].addEventListener("click", function() {
      console.log("Clicked Button: " + index);

      let isPresent = false;

      // Checks if the class is present or not
      this.classList.forEach(function(e, i) {
        if (e == "focus") {
          isPresent = true;
        } else {
          isPresent = false;
        }
      });

      // Toggles the presence of class (.focus) on the basis of the isPresent variable
      if (isPresent) {
        this.classList.remove("focus");
      } else {
        this.classList.add("focus");
      }
    });
  })(i);
}
.focus {
  background-color: #7d698d;
  color: white;
  cursor: pointer;
}
<button id="btn">Click</button>
<button id="btn">Click</button>
<button id="btn">Click</button>

No need for js. Use the focus selector in your stylesheet.

The :focus selector is allowed on elements that accept keyboard events or other user inputs.

~ W3

button.list-button:focus {
  background-color: #7d698d;
  color: white;
}
<button class="list-button">Click</button>

Edit ~ with js. This method uses .querySelectorAll("button") and toggles the styled class .focus each time the button(s) are clicked. This works well when having multiple buttons.

// Get all the elements on the basis of query selector (button)
let btn = document.querySelectorAll("button");

for (var i = 0; i < btn.length; i++) {
  (function(index) {
    btn[index].addEventListener("click", function() {
      console.log("Clicked Button: " + index);

      let isPresent = false;

      // Checks if the class is present or not
      this.classList.forEach(function(e, i) {
        if (e == "focus") {
          isPresent = true;
        } else {
          isPresent = false;
        }
      });

      // Toggles the presence of class (.focus) on the basis of the isPresent variable
      if (isPresent) {
        this.classList.remove("focus");
      } else {
        this.classList.add("focus");
      }
    });
  })(i);
}
.focus {
  background-color: #7d698d;
  color: white;
  cursor: pointer;
}
<button id="btn">Click</button>
<button id="btn">Click</button>
<button id="btn">Click</button>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文