gpt4 book ai didi

Javascript 'click' 按钮只能工作一次,必须刷新页面

转载 作者:行者123 更新时间:2023-12-05 07:14:40 26 4
gpt4 key购买 nike

所以我正在处理这个交互式网格并设置它,所以当我单击一个按钮时,网格 div 会根据所选按钮更改背景颜色。我的问题是我只能单击每个按钮一次,然后在我刷新页面之前它不会工作。我该如何解决这个问题,以便可以根据需要多次更改颜色?

HTML

<!DOCTYPE html>
<html>
<head>
<title> Claudias Etch-A-Sketch</title>
<link rel= "stylesheet" href="style.css">
</head>


<body>
<section class="back">
<h1><center> Claudia Etch-A-Sketch!</center></h1>
</section>

<section>
<div>
<button class="Black">Black</button>
<button class="Random">Random Color</button>
<button class="Clear">Clear Board</button>
</div>
</section>
<section>

<div id = "container"> </div>

</section>
</body>

<script src ="javascript.js"> </script>
</html>

JavaScript

const container = document.getElementById("container");
const btn= document.querySelector('button');


let y = document.querySelectorAll('button');
y.forEach(button => {
button.addEventListener('click', () => {
let choice = button.innerHTML;
switch (choice) {
case "Random Color":
random();
break;
case "Black":
blackchange();
break;
case "Clear Board":
reset();
break;
}
});
})


function blackchange() { const bb = container.addEventListener('mouseover', e=> e.target.classList.add('black'))};


function random() {
const rr= container.addEventListener('mouseover', e=> e.target.classList.add('random'))
};

function reset () { const rr= container.addEventListener('mouseover', e => e.target.classList.add('reset'))};

function makeRows(rows, cols) {
container.style.setProperty('--grid-rows', rows);
container.style.setProperty('--grid-cols', cols);
for (c = 0; c < (rows * cols); c++) {
let cell = document.createElement("div")


container.appendChild(cell).className = "griditem"

};
};

makeRows(16, 16);

最佳答案

您忘记了您对页面所做的一切都必须清理干净,否则您所做操作的影响将仍然存在。

当您设置一个新的mouseover 函数时,您需要先使用removeEventListener 移除 mouseover 函数.在添加新类之前,您还需要删除用户过去可能通过鼠标悬停添加到元素的其他类。

试试这个:

const container = document.getElementById("container");
const btn = document.querySelector('button');


let y = document.querySelectorAll('button');
y.forEach(button => {
button.addEventListener('click', () => {
let choice = button.innerHTML;
switch (choice) {
case "Random Color":
random();
break;
case "Black":
blackchange();
break;
case "Eraser":
reset();
break;
}
});
});

var currentMouseoverFunction = function() {};

function blackchange() {
container.removeEventListener('mouseover', currentMouseoverFunction);
currentMouseoverFunction = function(e) {
e.target.classList.remove('random');
e.target.classList.remove('reset');
e.target.classList.add('black');
};
container.addEventListener('mouseover', currentMouseoverFunction);
};

function random() {
container.removeEventListener('mouseover', currentMouseoverFunction);
currentMouseoverFunction = function(e) {
e.target.classList.remove('black');
e.target.classList.remove('reset');
e.target.classList.add('random');
};
container.addEventListener('mouseover', currentMouseoverFunction);
};

function reset() {
container.removeEventListener('mouseover', currentMouseoverFunction);
currentMouseoverFunction = function(e) {
e.target.classList.remove('black');
e.target.classList.remove('random');
e.target.classList.add('reset');
};
container.addEventListener('mouseover', currentMouseoverFunction);
};

function makeRows(rows, cols) {
container.style.setProperty('--grid-rows', rows);
container.style.setProperty('--grid-cols', cols);
for (c = 0; c < (rows * cols); c++) {
let cell = document.createElement("div");
container.appendChild(cell).className = "griditem";
};
};

makeRows(16, 16);
#container{
width:160px;
height:160px;
margin-top:10px;
background:#eee;
}

#container div {
float:left;
height: 10px;
width: 10px;
}

.black {
background: black;
}

.random {
background: pink;
}

.reset {
background: transparent;
}
<section class="back">
<h1>
<center> Claudia Etch-A-Sketch!</center>
</h1>
</section>
<section>
<div>
<button class="Black">Black</button>
<button class="Random">Random Color</button>
<button class="Clear">Eraser</button>
</div>
</section>
<section>
<div id="container"> </div>
</section>

我出于测试目的添加了自己的简单 CSS,因为我无法访问您的。

关于Javascript 'click' 按钮只能工作一次,必须刷新页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59805956/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com