简体   繁体   English

如何重置按键时的背景颜色?

[英]How can I reset background color on keypress?

I'm trying to have divs that, when moused over, change their background color, and being able to reset those colors. I've managed the former, but the latter is giving me issues.我正在尝试让 div 在鼠标悬停时更改其背景颜色,并能够重置那些 colors。我已经管理了前者,但后者给我带来了问题。

As written, the divs do, in fact, change color, but attempting to press a key does not result in anything.如所写,div 实际上会改变颜色,但尝试按下某个键不会产生任何结果。 I've also tried to put the resetColor function in the same block of Script, but doing so makes it so I can't even hover over and change the colors of those divs.我还尝试将 resetColor function 放在同一脚本块中,但这样做使得我什至无法将 hover 改过来并更改那些 div 的 colors。 As such, I'm wondering what I should do to be able to reset the divs' color?因此,我想知道我应该怎么做才能重置 div 的颜色?

 function randomColor(element) { var x = Math.floor(Math.random() * 256); var y = Math.floor(Math.random() * 256); var z = Math.floor(Math.random() * 256); var bgColor = "rgb(" + x + "," + y + "," + z + ")"; console.log(bgColor); element.backgroundColor = bgColor; } function resetColor(element) { element.backgroundColor = '#ffff99'; }
 body { background-color: lightblue; }.colorCell { border: 1px solid black; width: 10px; padding: 5px; background-color: #ffff99; border-radius: 0px; }
 <div class="colorCell" onmouseover="randomColor(this.style)" onkeydown="resetColor(this.style)"> <.--Successfullly changed color--> <.--<a onmouseover="colorCell.body.style.backgroundColor = '#f39352'">--> </div> <div class="colorCell" onmouseover="randomColor(this.style)" onkeydown="resetColor(this.style)"> </div> <div class="colorCell" onmouseover="randomColor(this.style)" onkeydown="resetColor(this.style)"> </div>

Check the fiddle: https://jsfiddle.net/ycn4zx9b/3/查看小提琴: https://jsfiddle.net/ycn4zx9b/3/

HTML: HTML:

<div class="colorCell" onmouseover="randomColor(this)"></div>
<div class="colorCell" onmouseover="randomColor(this)"></div>
<div class="colorCell" onmouseover="randomColor(this)"></div>

CSS is still the same as you provided. CSS 仍然与您提供的相同。

JS:记者:

function randomColor(element) {
 var x = Math.floor(Math.random() * 256);
 var y = Math.floor(Math.random() * 256);
 var z = Math.floor(Math.random() * 256);
 var bgColor = "rgb(" + x + "," + y + "," + z + ")";
 element.style.backgroundColor = bgColor;
 element.classList.add(`reset-me`);
}

function resetColor(element) {
 element.style.backgroundColor = '#ffff99';
}

window.onload = () => {
 document.getElementsByTagName(`body`)[0].addEventListener(`keydown`, () => {
  var itemss = document.querySelectorAll(`.reset-me`);
  for (var i = 0; i < itemss.length; i++) {
   resetColor(itemss[i]);
  }
 });
}

Keypress won't work on such elements on which you cannot enter something. Keypress 不适用于您无法输入内容的此类元素。 Make the event global instead and it should work.改为将事件设置为全局事件,它应该可以工作。

an idea can be to:一个想法可以是:

  • add an event listener at document level在文档级别添加事件监听器
  • stored hovered cell存储的悬停单元格
  • if one cell is over and keydown reset the celle style如果一个单元格结束并且 keydown 重置单元格样式

 let elementHovered = null; function getRandomColor() { return Math.floor(Math.random() * 256); } function randomColor(element) { const bgColor = `rgb(${getRandomColor()}, ${getRandomColor()}, ${getRandomColor()})`; console.log(bgColor); element.backgroundColor = bgColor; } function resetColor(element) { element.backgroundColor = '#ffff99'; } [...document.querySelectorAll('.colorCell')].forEach(cell => { cell.addEventListener('mouseover', () => { elementHovered = event.target.style; randomColor(elementHovered); }); }); document.addEventListener('keydown', () => { if (elementHovered) { resetColor(elementHovered) } });
 body { background-color: lightblue; }.colorCell { border: 1px solid black; width: 10px; padding: 5px; background-color: #ffff99; border-radius: 0px; }
 <div class="colorCell"></div> <div class="colorCell"></div> <div class="colorCell"></div>

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

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