简体   繁体   English

JavaScript按钮在第二次单击时不起作用

[英]JavaScript button doesn't work on second click

I want to make a program changing background color of a div after clicking on a button. 我想制作一个在单击按钮后更改div背景颜色的程序。

After first click it should change one color to the second one. 第一次单击后,应将一种颜色更改为第二种颜色。 After second click the color should come back to the first option. 第二次单击后,颜色应回到第一个选项。 And for the last time, the color should be switched again to the second option. 最后一次,颜色应再次切换到第二个选项。 So it should work for 3 times. 因此,它应该工作3次。 But in my code it works just for the first click. 但是在我的代码中,它仅适用于首次点击。

What did I do wrong? 我做错了什么?

 var btn = document.getElementById('button'); var box = document.getElementById('sq'); function changeColor() { var isPink = true; var colorA = "#BA498B"; var colorB = "#5964E3"; var i = 0; while (i < 3) { if (isPink) { change(colorB); isPink = false; i++; } else { change(colorA); isPink = true; i++; } } } function change(color) { btn.addEventListener('click', function () { box.style.backgroundColor = color; }); } window.onload = changeColor; 

You're adding a new, identical event handler every time there's a click. 每次单击时,您都将添加一个新的,相同的事件处理程序。 Instead just add it once when the page loads, so that the redundant handlers don't cancel each other out. 而是在页面加载时仅添加一次,这样冗余处理程序就不会互相抵消。

 var isPink = true; function changeColor() { var colorA = "#BA498B"; var colorB = "#5964E3"; if (isPink) { change(colorB); } else { change(colorA); } isPink = !isPink; } function change(color) { document.getElementById('sq').style.backgroundColor = color; } window.onload = function() { document.getElementById('button').addEventListener('click', changeColor); }; 
 #sq { width: 100px; height: 100px; position: relative; } 
 <button id=button>CLICK</button> <div id=sq></div> 

Don't know what you wanted with the loop though, so I removed it. 虽然不知道您想要的循环,所以我将其删除。 It runs immediately, and so you'd never see such a rapid color change. 它会立即运行,因此您永远不会看到如此快速的颜色变化。

try this: 尝试这个:

var isPink = true;
var color = "#BA498B"

function changeColor() {

    var colorA = "#BA498B";
    var colorB = "#5964E3";


        if (isPink) {
            color = (colorB);
            isPink = false;      
        }  else {
            color = (colorA);
            isPink = true;
        } 
}



window.onload = function(){
    var btn = document.getElementById('button');
    var box = document.getElementById('sq');

    btn.addEventListener('click', function () {
        changeColor();
        box.style.backgroundColor = color;
    });
};

This way I'm only using 'addEventListener' on the button once 这样,我只在按钮上使用了一次'addEventListener'

Here is an example which may help you: 这是一个可以帮助您的示例:

 var btn = document.getElementById('button'); var box = document.getElementById('sq'); var isPink = true; function changeColor() { var colorA = "#BA498B"; var colorB = "#5964E3"; var color=isPink?colorA:colorB; isPink=isPink?false:true; return color; } function change() { box.style.backgroundColor = changeColor(); } btn.addEventListener('click', change); window.onload = change(); 
 <h1 id="sq">Test Text for Example</h1> <button id="button">Click</button> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM