简体   繁体   English

如何设置 div onclick 的背景?

[英]How do I set the background of a div onclick?

I'm trying to set the background of a div when it is clicked, and then when it is clicked again it reverses back to no color.我试图在单击时设置 div 的背景,然后再次单击它时它又恢复为无颜色。 Here is my code:这是我的代码:

<div class="wrapper">
        <div onclick="set()" id="square"></div>
        <div onclick="set()" id="square"></div>
        <div onclick="set()" id="square"></div>
        <div onclick="set()" id="square"></div>
        <div onclick="set()" id="square"></div>
        <div onclick="set()" id="square"></div>
        <div onclick="set()" id="square"></div>
</div>
.wrapper {
    display: grid;
    grid-template-columns: repeat(107, 1fr);
    grid-template-rows: repeat(59, 1fr);
    grid-gap: 0px;

}

.wrapper div {
    padding: 5px;
    background-color: none;
    text-align: center;
    border: 0.001px solid black;
    font-size: 5px;
    display: flex;
    justify-content: center;
    align-items: center;
}

.wrapper div:hover {
    background: white;
}
function set() {

    var squares = document.getElementById('square');
    for (var i = 0; i < squares.length; i++) {
        squares[i].style.backgroundColor = "none";
    }

    if (squares[i].style.backgroundColor === "none") {
        squares[i].style.backgroundColor = "white";
    } else {
        squares[i].style.backgroundColor = "none";
    }
}

When I click the div it doesn't do anything.当我点击 div 时,它什么也没做。 My javascript code isn't working, how should I change it so it makes the div background white when clicked and then none when clicked again.我的 javascript 代码不工作,我应该如何更改它,以便在单击时使 div 背景变为白色,然后再次单击时使 div 背景变为白色。

You can put an event listener on the parent element that listens for the click event.您可以在监听点击事件的父元素上放置一个事件监听器。 When it gets a click event, if it is from one of the squares, toggle a class to keep the background color on it.当它获得点击事件时,如果它来自其中一个方块,则切换 class 以保持其背景颜色。 If it already has that class, it will be removed.如果它已经有 class,它将被删除。

 document.querySelector('.squares').addEventListener('click', e => { if (e.target.classList.contains('square')) { e.target.classList.toggle('selected'); } });
 .wrapper { display: grid; grid-template-columns: repeat(107, 1fr); grid-template-rows: repeat(59, 1fr); grid-gap: 0px; }.wrapper div { padding: 5px; background-color: none; text-align: center; border: 0.001px solid black; font-size: 24px; display: flex; justify-content: center; align-items: center; }.wrapper.square:hover, .square.selected { background: white; } body { background-color: #888; }
 <div class="wrapper squares"> <div class="square">A</div> <div class="square">B</div> <div class="square">C</div> <div class="square">D</div> <div class="square">E</div> <div class="square">F</div> <div class="square">G</div> </div>

Your primary issue is this:您的主要问题是:

<div onclick="set()" id="square"></div>
<div onclick="set()" id="square"></div>
<div onclick="set()" id="square"></div>
<div onclick="set()" id="square"></div>
<div onclick="set()" id="square"></div>
<div onclick="set()" id="square"></div>
<div onclick="set()" id="square"></div>

It's invalid to have the same id on more than one element.在多个元素上使用相同的id是无效的。 An id must be unique. id必须是唯一的。 So this line:所以这一行:

var squares = document.getElementById('square');

just selects the first div and then later, when you try to loop over all the cells, you don't because you never had all of the cells in the first place.只选择第一个div然后稍后,当您尝试遍历所有单元格时,您不会这样做,因为您从来没有首先拥有所有单元格。

You just need to set one click handler on the parent of all the div cells.您只需要在所有div单元格的父级上设置一个click处理程序。 Then, in that handler, you can apply a CSS class to the div that triggered the event.然后,在该处理程序中,您可以将 CSS class 应用于触发事件的div This is called event delegation and works because events bubble up through the DOM.这称为事件委托,并且之所以有效,是因为事件通过 DOM 冒泡。

 // Get a reference to all the cells let squares = document.querySelectorAll("div.wrapper > div"); // Set up you event handlers in JavaScipt, not with inline HTML // event attributes like onclick document.querySelector(".wrapper").addEventListener("click", function(event) { // Remove all the background colors from all the cells squares.forEach(function(square){ square.classList.remove("activeCell"); }); // Apply backgroun to clicked cell event.target.classList.add("activeCell"); });
 .wrapper { display: grid; grid-template-columns: repeat(107, 1fr); grid-template-rows: repeat(59, 1fr); grid-gap: 0px; }.wrapper > div { padding: 5px; background-color: none; text-align: center; border: 0.001px solid black; font-size: 5px; display: flex; justify-content: center; align-items: center; }.wrapper div:hover { background: green; }.activeCell { background-color:blue; }
 <div class="wrapper"> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> </div>

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

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