点击按钮时两次更改按钮的颜色。

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

Change Button's color twice when it's clicked

问题

我正在尝试创建一个按钮,当它被点击时,会更改背景颜色(变为绿色)。但当再次点击它时,按钮会返回到原始的背景颜色(橙色)。

  1. var btn1 = document.getElementById("btn-1");
  2. if (btn1.style.backgroundColor === "orange") {
  3. btn1.addEventListener("click", function () {
  4. btn1.style.backgroundColor = "green";
  5. });
  6. } else {
  7. btn1.addEventListener("click", function () {
  8. btn1.style.backgroundColor = "orange";
  9. });
  10. }

能帮到你吗?谢谢!

英文:

I'm trying to create a button whose change background color (to green) when it is cliked. But when it is cliked againg the button returns to the original background color (from orange).

  1. var btn1 = document.getElementById("btn-1")
  2. if (btn1.style.backgroundColor = "orange") {
  3. btn1.addEventListener("click", function () {
  4. btn1.style.backgroundColor = "green"
  5. })
  6. } else {btn1.addEventListener("click", function () {
  7. btn1.style.backgroundColor = "orange"
  8. })
  9. }

Could you help me? Thx!

I'm trying to create a button whose change background color (to green) when it is cliked. But when it is cliked againg the button returns to the original background color (from orange).

答案1

得分: 2

有许多方法可以做到这一点。我首选的方法是简单地切换元素上的一个类。

在 CSS 中通过 #btn-1 设置默认颜色,然后通过 #btn-1.active 设置活动颜色。然后通过切换活动类,当元素具有活动类时,将颜色更改为绿色,没有活动类时为橙色。

英文:

There are many ways to do this. My preferred way is simply toggle a class on the element.

In CSS set the default color via #btn-1 then have an active color set via #btn-1.active. Then by toggling the class of active, you change the color to green when the element has the class of active, and orange when it doesn't.

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

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

  1. var toggleBTN = document.getElementById(&quot;btn-1&quot;)
  2. toggleBTN.addEventListener(&quot;click&quot;,() =&gt; {
  3. toggleBTN.classList.toggle(&quot;active&quot;);
  4. });

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

  1. #btn-1{
  2. background:orange;
  3. }
  4. #btn-1.active{
  5. background:green;
  6. }

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

  1. &lt;button id=&quot;btn-1&quot;&gt;Toggle Color&lt;/button&gt;

<!-- end snippet -->

答案2

得分: 1

只需在单击事件上切换颜色:

  1. <button id="btn-1" style="background-color: orange">点击我</button>
  1. document
  2. .getElementById("btn-1")
  3. .addEventListener(
  4. 'click',
  5. ({target:{style}}) => style.backgroundColor = style.backgroundColor === 'orange' ? 'green' : 'orange'
  6. );
英文:

Just toggle the color on the click event:

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

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

  1. document
  2. .getElementById(&quot;btn-1&quot;)
  3. .addEventListener(
  4. &#39;click&#39;,
  5. ({target:{style}}) =&gt; style.backgroundColor = style.backgroundColor === &#39;orange&#39; ? &#39;green&#39; : &#39;orange&#39;
  6. );

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

  1. &lt;button id=&quot;btn-1&quot; style=&quot;background-color: orange&quot;&gt;Click me&lt;/button&gt;

<!-- end snippet -->

答案3

得分: 0

以下是翻译好的部分:

  1. let button = document.getElementById("button");
  2. button.style.backgroundColor = "orange";
  3. button.addEventListener("click", function () {
  4. if (button.style.backgroundColor == "orange") {
  5. button.style.backgroundColor = "green";
  6. } else button.style.backgroundColor = "orange";
  7. })
  1. <button id="button">test</button>
英文:

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

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

  1. let button = document.getElementById(&quot;button&quot;);
  2. button.style.backgroundColor = &quot;orange&quot;;
  3. button.addEventListener(&quot;click&quot;, function () {
  4. if(button.style.backgroundColor == &quot;orange&quot;){
  5. button.style.backgroundColor = &quot;green&quot;;
  6. } else button.style.backgroundColor = &quot;orange&quot;;
  7. })

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

  1. &lt;button id=&quot;button&quot;&gt;test&lt;/button&gt;

<!-- end snippet -->

how i understand you: you can set starter color of button to orange;<br><br>
and then add EventListener to button with this logic:<br>
-if the color of the button is orange - change to green<br>
or if the color is not orange - change to orange<br>

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

发表评论

匿名网友

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

确定