gpt4 book ai didi

javascript - 表格列在悬停时改变颜色

转载 作者:行者123 更新时间:2023-11-28 17:25:24 26 4
gpt4 key购买 nike

我一直在使用我在网上找到的一系列模板拼凑出表格格式(我自己不是熟练的程序员)。我正在尝试创建一个表格,当您将鼠标悬停在特定表格单元格上时,该表格将突出显示整个行和列,同时它还包括固定顶行的滚动功能。我已经弄清楚如何突出显示该行,但在处理该列时遇到困难。

我创建了一个 JSFiddle,它应该可以揭示问题:https://jsfiddle.net/n2p0zj6m/4/

我使用下面的代码在将鼠标悬停在表格单元格上时将相关类添加到表格中。我非常确定错误出在这段代码的某个地方,但我不明白为什么这不起作用。

  (function ($) {
"use strict";
$('.column100').on('mouseover',function(){
var table1 = $(this).parent().parent().parent();
var table2 = $(this).parent().parent();
var verTable = $(table1).data('vertable')+"";
var column = $(this).data('column') + "";

$(table2).find("."+column).addClass('hov-column-'+ verTable);
$(table1).find(".row100.head ."+column).addClass('hov-column-head-'+ verTable);
});

$('.column100').on('mouseout',function(){
var table1 = $(this).parent().parent().parent();
var table2 = $(this).parent().parent();
var verTable = $(table1).data('vertable')+"";
var column = $(this).data('column') + "";

$(table2).find("."+column).removeClass('hov-column-'+ verTable);
$(table1).find(".row100.head ."+column).removeClass('hov-column-head-'+ verTable);
});
})(jQuery);

我还将包含以下 HTML 结构的片段:

  <div class="tbl-header">
<table data-vertable="ver1" cellpadding="0" cellspacing="0" border="0">
<thead>
<tr class="row100 head">
<th class="column100 column1" data-column="column1">Code</th>
<th class="column100 column2" data-column="column2">Company</th>
<th class="column100 column3" data-column="column3">Price</th>
<th class="column100 column4" data-column="column4">Change</th>
<th class="column100 column5" data-column="column5">Change %</th>
</tr>
</thead>
</table>
</div>
<div class="tbl-content">
<table data-vertable="ver1" cellpadding="0" cellspacing="0" border="0">
<tbody>
<tr class="row100">
<td class="column100 column1" data-column="column1">AAC</td>
<td class="column100 column2" data-column="column2">AUSTRALIAN COMPANY </td>
<td class="column100 column3" data-column="column3">$1.38</td>
<td class="column100 column4" data-column="column4">+2.01</td>
<td class="column100 column5" data-column="column5">-0.36%</td>
</tr>

最佳答案

有一件事是,没有必要将表的各个级别分成不同的 divs - 这会产生问题,并且会使任何机器人/辅助技术感到困惑。在下面的代码片段中,我将其合并到一个表中。使用theadtbody相反。

此外,通过我使用的方法(收集单元格的 index - 因此其列),不需要额外的 data-xclasses用于跟踪。如果您的目标是出于样式目的,您应该查看 :nth-child()创建交替行等。

$('tbody td').on('mouseover', function() { // Triggers when a table cell is hovered under a tbody
column = $(this).index(); // Find which column the cell belongs to (its index amongst its siblings)

$(this).parent().siblings().each(function() { // loop over all rows except current (the siblings)
$(this).children().eq(column).addClass('colHover'); // addClass to the cell in the same column
});
});

$('tbody td').on('mouseout', function() { // Triggers when leaving a table cell
$(this).parent().siblings().each(function() {
$(this).children().removeClass('colHover'); // removeClass from all children, of all siblings
});
});
table thead tr {
background-color: #AAA;
}

table td,
table th {
padding: .5em .75em;
}

table tr td:nth-child(n+2) {
text-align: center;
}

table tbody tr:hover,
table tbody tr td.colHover {
background-color: #FFDDFF;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="tblFinance">
<table data-vertable="ver1" cellpadding="0" cellspacing="0" border="0">
<thead>
<tr>
<th>Code</th>
<th>Company</th>
<th>Price</th>
<th>Change</th>
<th>Change %</th>
</tr>
</thead>
<tbody>
<tr class="row100">
<td>AAC</td>
<td>AUSTRALIAN COMPANY </td>
<td>$1.38</td>
<td>+2.01</td>
<td>-0.36%</td>
</tr>
<tr class="row101">
<td>AAC</td>
<td>AUSTRALIAN COMPANY </td>
<td>$1.38</td>
<td>+2.01</td>
<td>-0.36%</td>
</tr>
<tr class="row102">
<td>AAC</td>
<td>AUSTRALIAN COMPANY </td>
<td>$1.38</td>
<td>+2.01</td>
<td>-0.36%</td>
</tr>
<tr class="row103">
<td>AAC</td>
<td>AUSTRALIAN COMPANY </td>
<td>$1.38</td>
<td>+2.01</td>
<td>-0.36%</td>
</tr>
</tbody>
</table>
</div>

编辑:我构建了一个额外的函数,它将事件附加到滚动。一旦用户滚动到(或经过)表格顶部,<tr>里面<thead>变得“粘性” - 它适用 position: fixed ,并调整表格以处理缺失的行,而不会弹跳页面。

注意:在目前的形式下,这不是非常可扩展的。它只会真正支持特定表中的单个“粘性”元素。

$(document).ready(() => {
var tblObject = $('table#tblFinance');
var tblTop = tblObject.offset().top; // Find Table's offset from the top of the page
var tblSticky = false; // reduce redundant conditional spam

$(window).scroll(() => {
if (!tblSticky && $(document).scrollTop() >= tblTop) {
// If Sticky is OFF, and user scrolls to/below the table's top

tblObject.find('thead tr').css('position', 'fixed'); // Set Position: fixed
tblObject.css('padding-top', tblObject.find('thead tr').outerHeight() + 'px'); // Add padding to table, to account for <tr> being removed from the flow
tblSticky = true; // Enable Sticky

} else if (tblSticky && $(document).scrollTop() <= tblTop) {
// If Sticky is ON, and user scrolls to/above the table's top

tblObject.find('thead tr').css('position', 'static'); // Set Position: Static
tblObject.css('padding-top', '0'); // Remove padding; <tr> is back in the flow
tblSticky = false; //Disable Sticky

}
});
});

$('tbody td').on('mouseover', function() { // Triggers when a table cell is hovered under a tbody
column = $(this).index(); // Find which column the cell belongs to (its index amongst its siblings)

$(this).parent().siblings().each(function() { // loop over all rows except current (the siblings)
$(this).children().eq(column).addClass('colHover'); // addClass to the cell in the same column
});
});

$('tbody td').on('mouseout', function() { // Triggers when leaving a table cell
$(this).parent().siblings().each(function() {
$(this).children().removeClass('colHover'); // removeClass from all children, of all siblings
});
});
table {
text-align: center;
}

table thead tr {
top: 0;
background-color: #AAA;
}

table td,
table th {
padding: .5em .75em;
}

table td:first-child,
table th:first-child {
width: 80px;
}

table td:nth-child(2),
table th:nth-child(2) {
width: 300px;
}

table td:nth-child(n+3),
table th:nth-child(n+3) {
width: 100px;
}

table tbody tr:hover,
table tbody tr td.colHover {
background-color: #FFDDFF;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
<h1>Title</h1>
<p>Some content, to push the table down a bit.</p>

<table id="tblFinance" data-vertable="ver1" cellpadding="0" cellspacing="0" border="0">
<thead>
<tr>
<th>Code</th>
<th>Company</th>
<th>Price</th>
<th>Change</th>
<th>Change %</th>
</tr>
</thead>
<tbody>
<tr>
<td>AAA</td>
<td>AUSTRALIAN COMPANY </td>
<td>$1.38</td>
<td>+2.01</td>
<td>-0.36%</td>
</tr>
<tr>
<td>BBB</td>
<td>AUSTRALIAN COMPANY </td>
<td>$1.38</td>
<td>+2.01</td>
<td>-0.36%</td>
</tr>
<tr>
<td>CCC</td>
<td>AUSTRALIAN COMPANY </td>
<td>$1.38</td>
<td>+2.01</td>
<td>-0.36%</td>
</tr>
<tr>
<td>DDD</td>
<td>AUSTRALIAN COMPANY </td>
<td>$1.38</td>
<td>+2.01</td>
<td>-0.36%</td>
</tr>
<tr>
<td>AAC</td>
<td>AUSTRALIAN COMPANY </td>
<td>$1.38</td>
<td>+2.01</td>
<td>-0.36%</td>
</tr>
<tr>
<td>AAC</td>
<td>AUSTRALIAN COMPANY </td>
<td>$1.38</td>
<td>+2.01</td>
<td>-0.36%</td>
</tr>
<tr>
<td>AAC</td>
<td>AUSTRALIAN COMPANY </td>
<td>$1.38</td>
<td>+2.01</td>
<td>-0.36%</td>
</tr>
<tr>
<td>AAC</td>
<td>AUSTRALIAN COMPANY </td>
<td>$1.38</td>
<td>+2.01</td>
<td>-0.36%</td>
</tr>
<tr>
<td>AAC</td>
<td>AUSTRALIAN COMPANY </td>
<td>$1.38</td>
<td>+2.01</td>
<td>-0.36%</td>
</tr>
<tr>
<td>AAC</td>
<td>AUSTRALIAN COMPANY </td>
<td>$1.38</td>
<td>+2.01</td>
<td>-0.36%</td>
</tr>
<tr>
<td>AAC</td>
<td>AUSTRALIAN COMPANY </td>
<td>$1.38</td>
<td>+2.01</td>
<td>-0.36%</td>
</tr>
<tr>
<td>AAC</td>
<td>AUSTRALIAN COMPANY </td>
<td>$1.38</td>
<td>+2.01</td>
<td>-0.36%</td>
</tr>
<tr>
<td>AAC</td>
<td>AUSTRALIAN COMPANY </td>
<td>$1.38</td>
<td>+2.01</td>
<td>-0.36%</td>
</tr>
<tr>
<td>AAC</td>
<td>AUSTRALIAN COMPANY </td>
<td>$1.38</td>
<td>+2.01</td>
<td>-0.36%</td>
</tr>
<tr>
<td>AAC</td>
<td>AUSTRALIAN COMPANY </td>
<td>$1.38</td>
<td>+2.01</td>
<td>-0.36%</td>
</tr>
<tr>
<td>AAC</td>
<td>AUSTRALIAN COMPANY </td>
<td>$1.38</td>
<td>+2.01</td>
<td>-0.36%</td>
</tr>
<tr>
<td>AAC</td>
<td>AUSTRALIAN COMPANY </td>
<td>$1.38</td>
<td>+2.01</td>
<td>-0.36%</td>
</tr>
<tr>
<td>AAC</td>
<td>AUSTRALIAN COMPANY </td>
<td>$1.38</td>
<td>+2.01</td>
<td>-0.36%</td>
</tr>
<tr>
<td>AAC</td>
<td>AUSTRALIAN COMPANY </td>
<td>$1.38</td>
<td>+2.01</td>
<td>-0.36%</td>
</tr>
<tr>
<td>AAC</td>
<td>AUSTRALIAN COMPANY </td>
<td>$1.38</td>
<td>+2.01</td>
<td>-0.36%</td>
</tr>
<tr>
<td>AAC</td>
<td>AUSTRALIAN COMPANY </td>
<td>$1.38</td>
<td>+2.01</td>
<td>-0.36%</td>
</tr>
<tr>
<td>AAC</td>
<td>AUSTRALIAN COMPANY </td>
<td>$1.38</td>
<td>+2.01</td>
<td>-0.36%</td>
</tr>
<tr>
<td>AAC</td>
<td>AUSTRALIAN COMPANY </td>
<td>$1.38</td>
<td>+2.01</td>
<td>-0.36%</td>
</tr>
<tr>
<td>AAC</td>
<td>AUSTRALIAN COMPANY </td>
<td>$1.38</td>
<td>+2.01</td>
<td>-0.36%</td>
</tr>
<tr>
<td>AAC</td>
<td>AUSTRALIAN COMPANY </td>
<td>$1.38</td>
<td>+2.01</td>
<td>-0.36%</td>
</tr>
<tr>
<td>AAC</td>
<td>AUSTRALIAN COMPANY </td>
<td>$1.38</td>
<td>+2.01</td>
<td>-0.36%</td>
</tr>
<tr>
<td>AAC</td>
<td>AUSTRALIAN COMPANY </td>
<td>$1.38</td>
<td>+2.01</td>
<td>-0.36%</td>
</tr>
<tr>
<td>AAC</td>
<td>AUSTRALIAN COMPANY </td>
<td>$1.38</td>
<td>+2.01</td>
<td>-0.36%</td>
</tr>
<tr>
<td>AAC</td>
<td>AUSTRALIAN COMPANY </td>
<td>$1.38</td>
<td>+2.01</td>
<td>-0.36%</td>
</tr>
<tr>
<td>AAC</td>
<td>AUSTRALIAN COMPANY </td>
<td>$1.38</td>
<td>+2.01</td>
<td>-0.36%</td>
</tr>
<tr>
<td>AAC</td>
<td>AUSTRALIAN COMPANY </td>
<td>$1.38</td>
<td>+2.01</td>
<td>-0.36%</td>
</tr>
<tr>
<td>AAC</td>
<td>AUSTRALIAN COMPANY </td>
<td>$1.38</td>
<td>+2.01</td>
<td>-0.36%</td>
</tr>
<tr>
<td>AAC</td>
<td>AUSTRALIAN COMPANY </td>
<td>$1.38</td>
<td>+2.01</td>
<td>-0.36%</td>
</tr>
<tr>
<td>AAC</td>
<td>AUSTRALIAN COMPANY </td>
<td>$1.38</td>
<td>+2.01</td>
<td>-0.36%</td>
</tr>
<tr>
<td>AAC</td>
<td>AUSTRALIAN COMPANY </td>
<td>$1.38</td>
<td>+2.01</td>
<td>-0.36%</td>
</tr>
<tr>
<td>AAC</td>
<td>AUSTRALIAN COMPANY </td>
<td>$1.38</td>
<td>+2.01</td>
<td>-0.36%</td>
</tr>
<tr>
<td>AAC</td>
<td>AUSTRALIAN COMPANY </td>
<td>$1.38</td>
<td>+2.01</td>
<td>-0.36%</td>
</tr>
<tr>
<td>AAC</td>
<td>AUSTRALIAN COMPANY </td>
<td>$1.38</td>
<td>+2.01</td>
<td>-0.36%</td>
</tr>
<tr>
<td>AAC</td>
<td>AUSTRALIAN COMPANY </td>
<td>$1.38</td>
<td>+2.01</td>
<td>-0.36%</td>
</tr>
<tr>
<td>AAC</td>
<td>AUSTRALIAN COMPANY </td>
<td>$1.38</td>
<td>+2.01</td>
<td>-0.36%</td>
</tr>
</tbody>
</table>
</div>

关于javascript - 表格列在悬停时改变颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51755491/

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