简体   繁体   English

如何在鼠标按下时根据悬停在哪个单元格上更改 html 表格单元格的颜色?

[英]How to change color of an html table cell when mouse is down, based on which cell it is hovering over?

Essentially, I want a user to be able to click and hold down their mouse and scroll over any number of cells in the table, as soon as they scroll over a cell it should change color.本质上,我希望用户能够单击并按住鼠标并滚动表格中的任意数量的单元格,只要他们滚动一个单元格,它就会改变颜色。 The thing is that when the user regularly clicks a cell I want the cell to change color, and I have a separate event listener that does that.问题是,当用户定期单击一个单元格时,我希望该单元格改变颜色,并且我有一个单独的事件侦听器可以做到这一点。

This is my table in html这是我在 html 中的表

<table class="c-table" onmousedown="hoverColorFill()">

and this is the js function I made to try to handle the hover on mousedown situation I described this:这是我在mousedown情况下尝试处理hover的js function 我描述了这个:

function hoverColorFill(){
    elements = document.querySelectorAll(':hover');
    for(let i=0; i<elements.length; i++){
    elements[i].style.backgroundcolor = colorEl.value
       }
}

this is the code I have for when someone simply clicks I cell:这是当有人简单地单击我的单元格时我拥有的代码:

table.addEventListener('click', (event) => {
  const rows = document.querySelectorAll('tr');
  const rowsArray = Array.from(rows);
  const rowIndex = rowsArray.findIndex(row => row.contains(event.target));
  const columns = Array.from(rowsArray[rowIndex].querySelectorAll('td'));
  const columnIndex = columns.findIndex(column => column == event.target);
  document.querySelector(".c-table").tBodies[0].rows[rowIndex-1].cells[columnIndex].style.backgroundColor = colorEl.value
})

it doesn't seem that the hoverColorFill() function works, when I drag my mouse over my table the function gets called (it can print to the console) but it doesn't change the colors. hoverColorFill() function 似乎不起作用,当我将鼠标拖到我的桌子上时,function 被调用(它可以打印到控制台),但它不会改变 Z62848E3CE5804AA978B2553。 My click event listener function completely properly, but it does occasionally give this error: Uncaught TypeError: Cannot read properties of undefined (reading 'style')at HTMLTableElement.我的点击事件监听器 function 完全正确,但它偶尔会给出此错误:未捕获的类型错误:无法在 HTMLTableElement 读取未定义的属性(读取“样式”)。 but the function that is not working doesn't throw any errors.但是不工作的 function 不会引发任何错误。

Edit: the reason I am not using an eventListener here is because I couldn't figure out how to do it so that it pays attention to both the hover and the mouseover.编辑:我在这里不使用 eventListener 的原因是因为我无法弄清楚如何做到这一点,因此它同时关注 hover 和鼠标悬停。

The code for coloring a cell seems quite complex.为单元格着色的代码似乎相当复杂。

This snippet just adds a class to the element.此代码段只是将 class 添加到元素中。

There does however need to be a bit more complexity about remembering when the mouse is down, and therefore coloring is to take place on mousemove, and to remember when the mouse is up.然而,记住鼠标何时按下确实需要更复杂一些,因此着色发生在 mousemove 上,并记住鼠标何时抬起。

The up and down events might take place outside the table so are sensed on the whole document. up 和 down 事件可能发生在表格之外,因此可以在整个文档上感知。

Also, when moving the mouse when it is down the default in the browser is usually to add a background color (the user is thought to be doing a selection).此外,当鼠标向下移动时,浏览器中的默认设置通常是添加背景颜色(用户被认为是在进行选择)。 This snippet sets this to transparent for the table so it's easier to see the coloring as it is happening.此代码段将其设置为对表格透明,以便更容易看到正在发生的颜色。

 const table = document.querySelector('table'); let mouseIsDown = false; table.addEventListener('click', function() { event.target.classList.add('colorCell'); }); document.addEventListener('mousedown', function() { mouseIsDown = true; event.target.classList.add('colorCell'); }) document.addEventListener('mouseup', function() { mouseIsDown = false; }); table.addEventListener('mouseover', function() { if (mouseIsDown) event.target.classList.add('colorCell'); });
 td.colorCell { background-color: yellow; } table::selection, tr::selection, td::selection { background-color: transparent; }
 <table> <tr> <td>Cell</td> <td>Cell</td> <td>Cell</td> <td>Cell</td> </tr> <tr> <td>Cell</td> <td>Cell</td> <td>Cell</td> <td>Cell</td> </tr> <tr> <td>Cell</td> <td>Cell</td> <td>Cell</td> <td>Cell</td> </tr> <tr> <td>Cell</td> <td>Cell</td> <td>Cell</td> <td>Cell</td> </tr> </table>

colorTd() function checks if you've clicked on a td and then adds a class to it colorTd() function 检查你是否点击了一个td然后添加一个 class 到它
It'll active when you click on it or drag your mouse along when you're clicking当你点击它时它会被激活,或者当你点击它时拖动鼠标
Whether you're dragging your mouse while clicked is checked by onmousedown and onmouseup . onmousedownonmouseup检查是否在单击时拖动鼠标。 It is stored in mouseIsDown它存储在mouseIsDown

When you're mouse is over the table (determined by onmouseover ) and when mouseIsDown is true , colorTd() function will execute, giving td sa class当您将鼠标悬停在桌子上时(由onmouseover确定)并且当mouseIsDowntrue时, colorTd() function 将执行,给出td sa class

 const table = document.querySelector("table"); const className = "selected"; let mouseIsDown = false; const colorTd = (e) => (e.target.tagName = "TD" && e.target.classList.add("selected")); table.onclick = (e) => colorTd(e); document.onmousedown = (e) => { mouseIsDown = true; colorTd(e); }; document.onmouseup = () => (mouseIsDown = false); table.onmouseover = (e) => mouseIsDown && colorTd(e);
 td { cursor: pointer; font-size: 22px; } td.selected { background-color: lightblue; } table::selection, tr::selection, td::selection { background-color: transparent; }
 <table> <tr> <td>Cell</td> <td>Cell</td> <td>Cell</td> <td>Cell</td> <td>Cell</td> </tr> <tr> <td>Cell</td> <td>Cell</td> <td>Cell</td> <td>Cell</td> <td>Cell</td> </tr> <tr> <td>Cell</td> <td>Cell</td> <td>Cell</td> <td>Cell</td> <td>Cell</td> </tr> <tr> <td>Cell</td> <td>Cell</td> <td>Cell</td> <td>Cell</td> <td>Cell</td> </tr> <tr> <td>Cell</td> <td>Cell</td> <td>Cell</td> <td>Cell</td> <td>Cell</td> </tr> </table>

dude, just use :hover in css.伙计,只需在 css 中使用:hover hover。 check this out https://www.w3schools.com/csSref/sel_hover.asp看看这个https://www.w3schools.com/csSref/sel_hover.asp

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

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