更改单个元素的背景颜色?

huangapple go评论53阅读模式
英文:

Changing the Background color of a single element?

问题

我目前正在制作学校的时间表。我有大约50个相同类别但具有不同id的按钮,是否可以使用JavaScript函数更改特定id的按钮的颜色?

以下是两个示例按钮:

<button type="button" class="btn" onclick="openPopup1()" id="output1"><strong>+</strong></button>
<button type="button" class="btn" onclick="openPopup2()" id="output2"><strong>+</strong></button>

我不知道如何做,研究后也没有找到任何信息,所以希望你能帮助我 更改单个元素的背景颜色?

英文:

I am currently working on a timetable for school. I have about 50 buttons of the same class but with different id´s. is it possible to change the color of a specific Button with a specific id with a function in js?
&lt;button type=&quot;button&quot; class=&quot;btn&quot; onclick=&quot;openPopup1()&quot; id=&quot;output1&quot;&gt;&lt;strong&gt;+&lt;/strong&gt;&lt;/button&gt;
&lt;button type=&quot;button&quot; class=&quot;btn&quot; onclick=&quot;openPopup2()&quot; id=&quot;output2&quot;&gt;&lt;strong&gt;+&lt;/strong&gt;&lt;/button&gt;

These are two of the Buttons as an Example

i had no ideas how to do it and found nothing after research so i hope you can help me 更改单个元素的背景颜色?

答案1

得分: 2

document.getElementById("output1").style.backgroundColor="red"有什么问题?实质上是在按钮上设置内联样式。

英文:

What's wrong with document.getElementById("output1").style.backgroundColor="red"? Essentially sets an inline style on the button.

答案2

得分: 1

这可能有点过于复杂,可能不是你所需要的。但是,如果你将 this 传递给你的 onclick 函数,你可以在函数中引用该按钮。

在我的示例中,我给被点击的按钮添加了一个 active 类,并从任何其他已点击的按钮中移除了 active 类。

function deactiveBtns(){
  let activeBTNs = document.querySelectorAll(".btn.active");
  activeBTNs.forEach((btn) => {
    btn.classList.remove("active");
  });
}

function openPopup1(el){
 deactiveBtns();
 el.classList.toggle("active");
}

function openPopup2(el){
 deactiveBtns();
  el.classList.toggle("active");
}
.btn.active{
background:red;
}
<button type="button" class="btn" onclick="openPopup1(this)" id="output1"><strong>+</strong></button>
<button type="button" class="btn" onclick="openPopup2(this)" id="output2"><strong>+</strong></button>
英文:

This might be overkill and not what you are looking for. But if you pass this to your onclick function, you can reference that button in the function.

In my example, I add a class of active to whatever button was clicked and remove active from any other clicked buttons.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function deactiveBtns(){
  let activeBTNs = document.querySelectorAll(&quot;.btn.active&quot;);
  activeBTNs.forEach((btn) =&gt; {
    btn.classList.remove(&quot;active&quot;);
  });
}

function openPopup1(el){
 deactiveBtns();
 el.classList.toggle(&quot;active&quot;);
}

function openPopup2(el){
 deactiveBtns();
  el.classList.toggle(&quot;active&quot;);
}

<!-- language: lang-css -->

.btn.active{
background:red;
}

<!-- language: lang-html -->

&lt;button type=&quot;button&quot; class=&quot;btn&quot; onclick=&quot;openPopup1(this)&quot; id=&quot;output1&quot;&gt;&lt;strong&gt;+&lt;/strong&gt;&lt;/button&gt; &lt;button type=&quot;button&quot; class=&quot;btn&quot; onclick=&quot;openPopup2(this)&quot; id=&quot;output2&quot;&gt;&lt;strong&gt;+&lt;/strong&gt;&lt;/button&gt;

<!-- end snippet -->

答案3

得分: 0

你可以使用JS通过访问元素的ID并适当设置style属性来更改按钮的背景颜色。

document.getElementById("headerStore").setAttribute("style", "background-color: red;");

你可以在页面加载时执行这个操作,或者通过按钮点击触发它。你还可以使用CSS引用特定的ID来实现这个目的。

希望这对你有帮助!

英文:

You can change the background color of a button with JS by accessing the element by Id and setting the style attribute appropriately.

document.getElementById(&quot;headerStore&quot;).setAttribute(&quot;style&quot;, &quot;background-color: red;&quot;);

You can do it when the page is loaded, or trigger it with the click of a button. You can also reference specific items by ID with CSS as well.

Hopefully this helps!

huangapple
  • 本文由 发表于 2023年2月24日 06:02:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75550771.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定