首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CSS突出显示表格行和列无效

问CSS突出显示表格行和列无效
EN

Stack Overflow用户
提问于 2019-09-15 00:02:22
回答 1查看 71关注 0票数 0

我有4张桌子并排在一起,就像Highlight column and row in a table ::after and ::before not work里的那张一样

我的问题是,以那个问题中的表格为例,当鼠标从c移动到b或a(或从b移动到a)时,高亮显示停留在与c的行上,而不是移动到b或a(停留在与b的行上)。基本上,当鼠标从上到下,而不是从下到上时,它工作得很好。

我想知道怎么解决这个问题。因为我还会在表单元格被单击时将其突出显示,并记录哪个单元格被突出显示和未突出显示,所以问题有时会在单击时使错误的单元格突出显示,并使记录有某种程度的偏差。

我正在使用Chrome进行测试。

下面是一个例子

​

代码语言:javascript
复制
            th{
                font-weight: normal;
                align-content: center;
            }
            td {
                border: black solid;
                border-width: 1px 2px 1px 1px;
                padding: 5px;
                text-align: center;
                height:1.1cm;
                width:1.1cm;
                background-clip: padding-box

            }
            .col {
                width: 1.1cm;
            }
            .rotate {
                text-align: center;
                white-space: nowrap;
                 vertical-align: middle;
                width: 1.5em;
                }
            .rotate div {
                -moz-transform: rotate(-90.0deg);  /* FF3.5+ */
                -o-transform: rotate(-90.0deg);  /* Opera 10.5 */
                -webkit-transform: rotate(-90.0deg);  /* Saf3.1+, Chrome */
                filter:  progid:DXImageTransform.Microsoft.BasicImage(rotation=0.083);  /* IE6,IE7 */
                -ms-filter: "progid:DXImageTransform.Microsoft.BasicImage(rotation=0.083)"; /* IE8 */
                margin-left: -1cm;
                margin-right: -1cm;
                }
            .float-left {
                width: 25%;
                float: left;
            }
            tr.hl:hover th:not([rowspan]),
            tr.hl:hover td {
                background-color: rgba(255,255,0,0.25);
            }
            td:hover::before {
                content: '';
                position: absolute;
                background-color: rgba(255,255,0,0.25);
                z-index: 0;
                width: 100%;
                top: 0;
            }
            td:hover::after,
            .col:hover::after {
                content: '';
                position: absolute;
                background-color: rgba(255,255,0,0.25);
                z-index: 0;
                height: 2000%;
                width: 100%;
                bottom: -1000%;
                left: 0;
            }
            th,
            td {
                position: relative;
            }
            table{
                overflow: hidden;

            }
            thead th{
                background-color:white;
                box-shadow: 0 0 0 2px white;
                z-index: 1;
            }
代码语言:javascript
复制
<div class="g1g2">
        <div class="float-left">
            <table id="player1" style="margin-bottom: 1cm">
             <thead>
        <tr>
            <th colspan=5 style="text-align: right" >Player 1's Payoffs </th>
        </tr>
        <tr>
            <th colspan=2 rowspan=2></th>
            <th colspan=3 style="font-size: smaller; text-align: center">Player 2's actions</th>
        </tr>
        </thead>
        <tr style="text-align: center">
             <th colspan="2"></th>
            <th class="col">d</th>
            <th class="col">e</th>
            <th class="col">f</th>
        </tr>


        <tr class="hl">
            <th rowspan=3 class="rotate" style="font-size: smaller;"> <div>Player 1's actions</div></th>
            <th>a</th>
            <td>8</td>
            <td>20</td>
            <td>12</td>
        </tr>

        <tr class="hl">
            <th>b</th>
            <td>0</td>
            <td>8</td>
            <td>16</td>
        </tr>
        <tr class="hl">
            <th>c</th>
            <td>18</td>
            <td>12</td>
            <td>6</td>
        </tr>
            </table>
        </div>
    </div>

    <div class="g1g2">
             <div class="float-left">
         <table id="player2" style="margin-bottom: 1cm">
             <thead>
             <tr>
            <th colspan=5 style="text-align: right">Player 2's Payoffs </th>
        </tr>
        <tr>
            <th colspan=2 rowspan=2></th>
            <th colspan=3 style="font-size: smaller; text-align: center">Player 3's actions</th>
        </tr>
             </thead>
        <tr style="text-align: center">
             <th colspan="2"></th>
            <th class="col">g</th>
            <th class="col">h</th>
            <th class="col">i</th>
        </tr>
        <tr class="hl">
            <th rowspan=3 class="rotate" style="font-size: smaller"> <div>Player 2's actions</div></th>
            <th>d</th>
            <td>14</td>
            <td>18</td>
            <td>4</td>
        </tr>
        <tr class="hl">
            <th>e</th>
            <td>20</td>
            <td>14</td>
            <td>8</td>
        </tr>
        <tr class="hl">
            <th>f</th>
            <td>16</td>
            <td>2</td>
            <td>18</td>
        </tr>
         </table>
     </div>
    </div>


    <div class="g1g2">
        <div class="float-left">
         <table id="player3" style="margin-bottom: 1cm">
             <thead>
             <tr>
                <th colspan=5 style="text-align: right">Player 3's Payoffs </th>
            </tr>
            <tr>
                <th colspan=2 rowspan=2></th>
                <th colspan=3 style="font-size: smaller; text-align: center">Player 4's actions</th>
            </tr>
             </thead>
             <tr style="text-align: center">
                  <th colspan="2"></th>
                <th class="col">j</th>
                <th class="col">k</th>
                <th class="col">l</th>
            </tr>
        <tr class="hl">
            <th rowspan=3 class="rotate" style="font-size: smaller"> <div>Player 3's actions</div></th>
            <th>g</th>
            <td>20</td>
            <td>14</td>
            <td>8</td>
        </tr>
        <tr class="hl">
            <th>h</th>
            <td>16</td>
            <td>2</td>
            <td>18</td>
        </tr>
        <tr class="hl">
            <th>i</th>
            <td>0</td>
            <td>16</td>
            <td>16</td>
        </tr>
         </table>
    </div>
    </div>

<div class="g1">
<div class="float-left">
         <table id="player4a" style="margin-bottom: 1cm">
              <thead>
             <tr>
                <th colspan=5 style="text-align: right">Player 4's Payoffs </th>
            </tr>
            <tr>
                <th colspan=2 rowspan=2></th>
                <th colspan=3 style="font-size: smaller; text-align: center">Player 1's actions</th>
            </tr>
             </thead>
            <tr style="text-align: center">
                 <th colspan="2"></th>
                <th class="col">a</th>
                <th class="col">b</th>
                <th class="col">c</th>
            </tr>
        <tr class="hl">
            <th rowspan=3 class="rotate" style="font-size: smaller"> <div>Player 4's actions</div></th>
            <th>j</th>
            <td>12</td>
            <td>16</td>
            <td>14</td>
        </tr>
        <tr class="hl">
            <th>k</th>
            <td>8</td>
            <td>12</td>
            <td>10</td>
        </tr>
        <tr class="hl">
            <th>l</th>
            <td>6</td>
            <td>10</td>
            <td>8</td>
        </tr>
         </table>
</div>
</div>

​

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-09-15 20:37:21

最简单的解决方法是向::after和::before伪元素添加pointer-events: none。It also has a great browser support。

Tbh我还没有看过代码xD,但可能另一个修复方法是摆弄z-index,我不推荐这样做,因为它可以用pointer-events解决。

​

代码语言:javascript
复制
            th{
                font-weight: normal;
                align-content: center;
            }
            td {
                border: black solid;
                border-width: 1px 2px 1px 1px;
                padding: 5px;
                text-align: center;
                height:1.1cm;
                width:1.1cm;
                background-clip: padding-box

            }
            .col {
                width: 1.1cm;
            }
            .rotate {
                text-align: center;
                white-space: nowrap;
                 vertical-align: middle;
                width: 1.5em;
                }
            .rotate div {
                -moz-transform: rotate(-90.0deg);  /* FF3.5+ */
                -o-transform: rotate(-90.0deg);  /* Opera 10.5 */
                -webkit-transform: rotate(-90.0deg);  /* Saf3.1+, Chrome */
                filter:  progid:DXImageTransform.Microsoft.BasicImage(rotation=0.083);  /* IE6,IE7 */
                -ms-filter: "progid:DXImageTransform.Microsoft.BasicImage(rotation=0.083)"; /* IE8 */
                margin-left: -1cm;
                margin-right: -1cm;
                }
            .float-left {
                width: 25%;
                float: left;
            }
            tr.hl:hover th:not([rowspan]),
            tr.hl:hover td {
                background-color: rgba(255,255,0,0.25);
            }
            td:hover::before {
                content: '';
                position: absolute;
                background-color: rgba(255,255,0,0.25);
                z-index: 0;
                width: 100%;
                top: 0;
            }
            td:hover::after,
            .col:hover::after {
                content: '';
                position: absolute;
                background-color: rgba(255,255,0,0.25);
                z-index: 0;
                height: 2000%;
                width: 100%;
                bottom: -1000%;
                left: 0;
                pointer-events: none;
            }
            th,
            td {
                position: relative;
            }
            table{
                overflow: hidden;

            }
            thead th{
                background-color:white;
                box-shadow: 0 0 0 2px white;
                z-index: 1;
            }
代码语言:javascript
复制
<div class="g1g2">
        <div class="float-left">
            <table id="player1" style="margin-bottom: 1cm">
             <thead>
        <tr>
            <th colspan=5 style="text-align: right" >Player 1's Payoffs </th>
        </tr>
        <tr>
            <th colspan=2 rowspan=2></th>
            <th colspan=3 style="font-size: smaller; text-align: center">Player 2's actions</th>
        </tr>
        </thead>
        <tr style="text-align: center">
             <th colspan="2"></th>
            <th class="col">d</th>
            <th class="col">e</th>
            <th class="col">f</th>
        </tr>


        <tr class="hl">
            <th rowspan=3 class="rotate" style="font-size: smaller;"> <div>Player 1's actions</div></th>
            <th>a</th>
            <td>8</td>
            <td>20</td>
            <td>12</td>
        </tr>

        <tr class="hl">
            <th>b</th>
            <td>0</td>
            <td>8</td>
            <td>16</td>
        </tr>
        <tr class="hl">
            <th>c</th>
            <td>18</td>
            <td>12</td>
            <td>6</td>
        </tr>
            </table>
        </div>
    </div>

    <div class="g1g2">
             <div class="float-left">
         <table id="player2" style="margin-bottom: 1cm">
             <thead>
             <tr>
            <th colspan=5 style="text-align: right">Player 2's Payoffs </th>
        </tr>
        <tr>
            <th colspan=2 rowspan=2></th>
            <th colspan=3 style="font-size: smaller; text-align: center">Player 3's actions</th>
        </tr>
             </thead>
        <tr style="text-align: center">
             <th colspan="2"></th>
            <th class="col">g</th>
            <th class="col">h</th>
            <th class="col">i</th>
        </tr>
        <tr class="hl">
            <th rowspan=3 class="rotate" style="font-size: smaller"> <div>Player 2's actions</div></th>
            <th>d</th>
            <td>14</td>
            <td>18</td>
            <td>4</td>
        </tr>
        <tr class="hl">
            <th>e</th>
            <td>20</td>
            <td>14</td>
            <td>8</td>
        </tr>
        <tr class="hl">
            <th>f</th>
            <td>16</td>
            <td>2</td>
            <td>18</td>
        </tr>
         </table>
     </div>
    </div>


    <div class="g1g2">
        <div class="float-left">
         <table id="player3" style="margin-bottom: 1cm">
             <thead>
             <tr>
                <th colspan=5 style="text-align: right">Player 3's Payoffs </th>
            </tr>
            <tr>
                <th colspan=2 rowspan=2></th>
                <th colspan=3 style="font-size: smaller; text-align: center">Player 4's actions</th>
            </tr>
             </thead>
             <tr style="text-align: center">
                  <th colspan="2"></th>
                <th class="col">j</th>
                <th class="col">k</th>
                <th class="col">l</th>
            </tr>
        <tr class="hl">
            <th rowspan=3 class="rotate" style="font-size: smaller"> <div>Player 3's actions</div></th>
            <th>g</th>
            <td>20</td>
            <td>14</td>
            <td>8</td>
        </tr>
        <tr class="hl">
            <th>h</th>
            <td>16</td>
            <td>2</td>
            <td>18</td>
        </tr>
        <tr class="hl">
            <th>i</th>
            <td>0</td>
            <td>16</td>
            <td>16</td>
        </tr>
         </table>
    </div>
    </div>

<div class="g1">
<div class="float-left">
         <table id="player4a" style="margin-bottom: 1cm">
              <thead>
             <tr>
                <th colspan=5 style="text-align: right">Player 4's Payoffs </th>
            </tr>
            <tr>
                <th colspan=2 rowspan=2></th>
                <th colspan=3 style="font-size: smaller; text-align: center">Player 1's actions</th>
            </tr>
             </thead>
            <tr style="text-align: center">
                 <th colspan="2"></th>
                <th class="col">a</th>
                <th class="col">b</th>
                <th class="col">c</th>
            </tr>
        <tr class="hl">
            <th rowspan=3 class="rotate" style="font-size: smaller"> <div>Player 4's actions</div></th>
            <th>j</th>
            <td>12</td>
            <td>16</td>
            <td>14</td>
        </tr>
        <tr class="hl">
            <th>k</th>
            <td>8</td>
            <td>12</td>
            <td>10</td>
        </tr>
        <tr class="hl">
            <th>l</th>
            <td>6</td>
            <td>10</td>
            <td>8</td>
        </tr>
         </table>
</div>
</div>

​

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57937149

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档