简体   繁体   English

纯Javascript表列的悬停效果如何?

[英]Pure Javascript table column hover effect?

I need a pure Javascript (no jQuery) hover effect for HTML table columns. 我需要HTML表列具有纯Javascript(无jQuery)悬停效果。

I found this which supposedly contains a fix for Firefox yet it still looks broken to me. 我发现这个据称包括用于Firefox修复但它仍然看起来坏了我。

I found this which works only for the first column. 我发现仅适用于第一列。

Unfortunately, my Javascript skills are amateur at best, so my attempts to modify either of these turned out to be fruitless. 不幸的是,我的JavaScript技能充其量是业余的,因此我尝试修改其中的任何一项都没有结果。

Is this possible? 这可能吗? Any suggestions would be appreciated. 任何建议,将不胜感激。

Here's a column-based approach. 这是一种基于列的方法。 When the mouse enters/leaves a cell, find the corresponding <col/> by index and apply/remove the desired class: 当鼠标进入/离开一个单元格时,通过索引找到相应的<col/>并应用/删除所需的类:

 (() => { const myTable = document.getElementById("myTable"); const cols = myTable.querySelectorAll("col"); const events = { mouseover: e => { const t = e.target.closest("td"); if (t) { const cellIndex = t.cellIndex; for (let i = 0, n = cols.length; i < n; i++) { cols[i].classList[i === cellIndex ? "add" : "remove"]("hovered"); } } }, mouseout: e => { const t = e.target; if (t.nodeName === "TD" && !t.contains(e.relatedTarget)) { cols[t.cellIndex].classList.remove("hovered"); } } }; for (let event in events) { myTable.addEventListener(event, events[event]); } })(); 
 .hovered { background-color: #FF0000; } 
 <table id="myTable" cellspacing="0"> <col /> <col /> <col /> <tbody> <tr> <td>Col1</td> <td>Col2</td> <td>Col3</td> </tr> <tr> <td>Col1</td> <td>Col2 <span>nested</span> </td> <td>Col3</td> </tr> <tr> <td>Col1</td> <td>Col2</td> <td>Col3</td> </tr> </tbody> </table> 

See also: 也可以看看:

Here are your codes (+ demo ): 这是您的代码(+ 演示 ):

var HOVER_CLASS = 'hovered';
var hovered;

table.addEventListener('mouseover', function (e) {
    if (e.target.tagName.toLowerCase() == 'td') {
        var index = e.target.cellIndex;

        hovered && hovered.forEach(function (cell) {
            cell.classList.remove(HOVER_CLASS);
        });

        hovered = Array.prototype.map.call(
            table.rows,
            function (row) {
                var i = index;
                while (!cell && i >= 0) {
                    var cell = row.cells[i];
                    i -= 1;
                }
                return cell;
            }
        );

        hovered.forEach(function (cell) {
            cell.classList.add(HOVER_CLASS);
        });
    }
}, true);

table.addEventListener('mouseout', function (e) {
    hovered && hovered.forEach(function (cell) {
        cell.classList.remove(HOVER_CLASS);
    });
    hovered = null;
}, true);

Best method I can think of is to give each <td> a class name that identifies the column it's in. ie "col1, col2, etc" 我能想到的最好的方法是给每个<td>一个类名,以标识它所在的列。即“ col1,col2等”

Then you can use the document.getElementsByClassName("colX") function to get an array of those <td> s, loop through the array and modify the style. 然后,您可以使用document.getElementsByClassName("colX")函数获取这些<td>的数组,遍历该数组并修改样式。 Warning, this may not work in older browsers that don't have a getElementsByClassName function, but there are workarounds you can find easily for that. 警告,这可能在没有getElementsByClassName函数的较旧的浏览器中不起作用,但是有一些变通办法可以轻松找到。 The best of which would be to use jQuery, not sure why you're against it. 最好的办法是使用jQuery,但不确定为什么要反对它。

You create a class in css 您在CSS中创建一个类

.HoverTabla > tbody > tr:hover,
.HoverTabla > tbody > tr:focus { 
    background-color: #42C6F7;
}

and then you call it from the table in the html 然后从html的表格中调用它

<table class="table HoverTabla" id="tbl_Plan">

            <thead>
                <tr>
                    <th>Tipo de plan</th>
                    <th>Tiempo en días</th>
                    <th>Max. Usuario</th>
                    <th>Max. Capacidad</th>
                    <th>Max. Casos</th>
                    <th>Valor plan</th>
                    <th></th>
                </tr>
            </thead>
 </table>

CSS-only answer I found after a little bit of googling: https://css-tricks.com/simple-css-row-column-highlighting/ 我经过一番谷歌搜索后发现了纯CSS答案: https : //css-tricks.com/simple-css-row-column-highlighting/

Each cell ( <td> ) in the table is given some padding through pseudo elements, which is used to create the hover effect. 表中的每个单元格( <td> )都通过伪元素进行了填充,用于创建悬停效果。 To make sure the hover effect doesn't extend further than the table itself, an overflow: hidden is used. 为了确保悬停效果不会超出表本身,请使用overflow: hidden

The sub-title in the article summarizes it all: "The trick is using huge pseudo elements on the <td> s, hidden by the table overflow" 本文中的小标题概括了这一切:“诀窍是在<td>上使用巨大的伪元素,隐藏在表溢出中”

尝试

<td onMouseOver="this.bgColor='yellow';" onMouseOut="this.bgColor='white';">

This will work, no javascript needed. 这将起作用,不需要使用javascript。 So it should work even when people turn javascript off. 因此,即使人们关闭了javascript,它也应该起作用。

Jfiddle: http://jsfiddle.net/vJacZ/ Jfiddle: http : //jsfiddle.net/vJacZ/

Html: HTML:

​<table>
<tr>
    <td class="column1">
        Column1
    </td>
    <td class="column2">
        Column2
    </td>
</tr>
</table>

Css: CSS:

.column1{
    color:black;
}
.column1:hover{
    color:red;
}
.column2{
    color:black;
}
.column2:hover{
    color:green;
}

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

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