【问题标题】:Cols, colgroups and css ":hover" pseudoclassCols、col 组和 css ":hover" 伪类
【发布时间】:2010-10-25 07:58:12
【问题描述】:

我正在尝试创建一个表格来显示个人的 BMI。

作为其中的一部分,我希望在:hover 上也突出显示<tr> <col>(或<colgroup>),以便交叉点更明显。

由于该表将同时具有公制和英制测量值,因此 :hover 不必(从任何方向)在单元格处停止,事实上,如果它从一个轴延伸到另一个轴,将是一个奖励。我也在使用 XHTML 1.1 Strict doctype,如果这有什么不同?

所以...一个例子(真正的桌子...更大),但这应该是有代表性的:

<script>

tr:hover {background-color: #ffa; }

colgroup:hover,
col:hover {background-color: #ffa; }

</script>

...

<table>
    <col class="weight"></col><colgroup span="3"><col class="bmi"></col></colgroup>

    <tr>
        <th></th>
        <th>50kg</th>
        <th>55kg</th>
        <th>60kg</th>
    </tr>

    <tr>
        <td>160cm</td>
        <td>20</td>
        <td>21</td>
        <td>23</td>
    </tr>

    <tr>
        <td>165cm</td>
        <td>18</td>
        <td>20</td>
        <td>22</td>
    </tr>

    <tr>
        <td>170cm</td>
        <td>17</td>
        <td>19</td>
        <td>21</td>
    </tr>

</table>

我是在问不可能吗,我需要去 JQuery-wards 吗?

【问题讨论】:

    标签: css xhtml html-table col


    【解决方案1】:

    无论如何,IE 都不支持TR 上的 AFAIK CSS 悬停,所以它的 TR 部分充其量只能在 Firefox 中使用。

    从未见过:hover 在 col/colgroup 上工作,所以不确定这是否可能......

    认为您可能会被 Javascript 实现所困。

    有一个示例 here 在 Firefox 中有效(行和列) 但它再次在 IE 中坏了...... cols 不起作用。

    【讨论】:

    • 这绝对符合我的经历,如果不是我所希望的奇迹的话...... =)
    • 取决于文档类型和版本。 ttp://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" rel="nofollow" target="_blank">w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 无论如何都可以在 IE7 中使用。
    • :hover 状态不仅适用于 Firefox,而且适用于所有其他主要的非 IE 浏览器(Opera、Konqueror、Safari 等)。
    【解决方案2】:

    我遇到了一个非常不错的 jQuery 插件located here,它通过大量示例在这类事情上做得非常好。我会优先使用它。

    【讨论】:

    • 不过,能够按照我的想象能够使用 CSS,这不是很好吗? =) 我会检查 JQuery,谢谢!
    【解决方案3】:

    这是一个不使用 JavaScript 的纯 CSS 方法。

    我使用::before::after 伪元素来突出显示行和列。 z-index&lt;td&gt;s 下方保持突出显示,以防您需要处理点击事件。 position: absolute 允许他们离开&lt;td&gt; 的范围。 overflow: hidden 上的 &lt;table&gt; 隐藏了高亮溢出。

    这不是必需的,但当您在标题中时,我也让它只选择行或列。 .row.col 类负责这一点。如果您希望简化,可以删除它们。

    这在所有现代浏览器中都有效(并且在旧浏览器上通过什么都不做来优雅地降级)。

    演示:http://jsfiddle.net/ThinkingStiff/rUhCa/

    输出:

    CSS:

    table {
        border-spacing: 0;
        border-collapse: collapse;
        overflow: hidden;
        z-index: 1;
    }
    
    td, th, .row, .col {
        cursor: pointer;
        padding: 10px;
        position: relative;
    }
    
    td:hover::before,
    .row:hover::before { 
        background-color: #ffa;
        content: '\00a0';  
        height: 100%;
        left: -5000px;
        position: absolute;  
        top: 0;
        width: 10000px;   
        z-index: -1;        
    }
    
    td:hover::after,
    .col:hover::after { 
        background-color: #ffa;
        content: '\00a0';  
        height: 10000px;    
        left: 0;
        position: absolute;  
        top: -5000px;
        width: 100%;
        z-index: -1;        
    }
    

    HTML:

    <table>
        <tr>
            <th></th>
            <th class="col">50kg</th>
            <th class="col">55kg</th>
            <th class="col">60kg</th>
            <th class="col">65kg</th>
            <th class="col">70kg</th>
        </tr>
        <tr>
            <th class="row">160cm</th>
            <td>20</td><td>21</td><td>23</td><td>25</td><td>27</td>
        </tr>
        <tr>
            <th class="row">165cm</th>
            <td>18</td><td>20</td><td>22</td><td>24</td><td>26</td>
        </tr>
        <tr>
            <th class="row">170cm</th>
            <td>17</td><td>19</td><td>21</td><td>23</td><td>25</td>
        </tr>
        <tr>
            <th class="row">175cm</th>
            <td>16</td><td>18</td><td>20</td><td>22</td><td>24</td>
        </tr>
    </table>
    

    【讨论】:

    • 我可以回复 +1 和接受吗?昨晚我玩弄了::before::after 伪元素(当我想起这个问题时),但完全可以让它们按我的意愿工作。谢谢! =)
    • 不再需要修复Firefox。
    • 您可能有兴趣知道这里有一个链接到您的答案的css-tricks article
    • 这在我测试的所有浏览器(IE9、IE11、FF、Chrome)中都非常有效——似乎不再需要 FF 修复 :-)
    • 您应该确保在 td 上设置了 overflow:visible!
    【解决方案4】:

    我从css-tricks.com 遇到了这种巧妙的做法,我还准备了一个小提琴,虽然没有什么花哨的东西,但你可以用那个 css-trick 页面提供的相同代码来理解它

    //HTML

    <table>
        <colgroup></colgroup>
        <colgroup></colgroup>
        <colgroup></colgroup>
        <colgroup></colgroup>
        <colgroup></colgroup>
        <thead>
            <tr>
                <th></th>
                <th></th>
                <th></th>
                <th></th>
                <th></th>
            </tr>
        </thead>
        <tbody>
                <tr>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                </tr>
                <tr>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                </tr>
                <tr>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                </tr>
            </tbody>
    </table>
    

    //Js

    $(function(){
        $("table").delegate('td','mouseover mouseleave', function(e) {
            if (e.type == 'mouseover') {
              $(this).parent().addClass("hover");
              $("colgroup").eq($(this).index()).addClass("hover");
            }
            else {
              $(this).parent().removeClass("hover");
              $("colgroup").eq($(this).index()).removeClass("hover");
            }
        });
    })
    

    Check out the fiddle here

    【讨论】:

      【解决方案5】:

      现场回答(https://jsfiddle.net/craig1123/d7105gLf/

      已经有 CSS 和 JQuery 的答案;但是,我写了一个简单的纯 javascript 答案。

      我首先找到所有coltd标签,通过element.cellIndex获取每个单元格的列索引,然后在mouseenter上添加一个带有背景的CSS类并在mouseleave上删除它.

      HTML

      <table id='table'>
        <col />
        <col />
        <col />
        <col />
        <thead>
          <tr>
            <th>Name</th>
            <th>Age</th>
            <th>Birthdate</th>
            <th>Preferred Hat Style</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td>Abraham Lincoln</td>
            <td>204</td>
            <td>February 12</td>
            <td>Stovepipe</td>
          </tr>
          <tr>
            <td>Winston Churchill</td>
            <td>139</td>
            <td>November 30</td>
            <td>Homburg</td>
          </tr>
          <tr>
            <td>Rob Glazebrook</td>
            <td>32</td>
            <td>August 6</td>
            <td>Flat Cap</td>
          </tr>
        </tbody>
      </table>
      

      CSS

      body {
        font: 16px/1.5 Helvetica, Arial, sans-serif;
      }
      
      table {
        width: 80%;
        margin: 20px auto;
        border-collapse: collapse;
      }
      table th {
        text-align: left;
      }
      table tr, table col {
        transition: all .3s;
      }
      table tbody tr:hover {
        background-color: rgba(0, 140, 203, 0.2);
      }
      table col.hover {
        background-color: rgba(0, 140, 203, 0.2);
      }
      tr, col {
        transition: all .3s;
      }
      tbody tr:hover {
        background-color: rgba(0,140,203,.2);
      }
      col.hover {
        background-color: rgba(0,140,203,.2);
      }
      

      JS

      const col = table.getElementsByTagName('col');
      const td = document.getElementsByTagName('td');
      
      const columnEnter = (i) => col[i].classList.add('hover');
      const columnLeave = (i) => col[i].classList.remove('hover');
      
      for (const cell of td) {
          const index = cell.cellIndex;
          cell.addEventListener('mouseenter', columnEnter.bind(this, index));
          cell.addEventListener('mouseleave', columnLeave.bind(this, index));
      }
      

      Here is a fiddle

      【讨论】:

        猜你喜欢
        • 2011-02-22
        • 2017-08-08
        • 1970-01-01
        • 2021-05-20
        • 2014-07-13
        • 1970-01-01
        • 2011-01-05
        • 2013-05-08
        • 2011-09-28
        相关资源
        最近更新 更多