【问题标题】:HTML table row linkHTML 表格行链接
【发布时间】:2010-10-08 20:01:58
【问题描述】:

使 HTML 表格的一行成为链接的最佳方法是什么?我目前正在使用 jquery 对行进行斑马条纹,并突出显示 onmouseover/off 选定的行,所以如果 JavaScript 是答案,请使用 jquery。

【问题讨论】:

  • 谷歌搜索查询'jquery tr link'已经给了我一些解决方案。也许你也应该试试?我没有回答你的问题,因为我根本没有 jquery 经验,所以我可能会说一些“愚蠢的”;-)
  • 是的,它会给你结果,但我不会考虑他们中的大多数答案。如果我用谷歌搜索“dog poop tr link”,我也会得到“答案”。 :)
  • 两个提议的 JQuery 解决方案在可用性和可访问性方面存在问题。 CSS 解决方案更适合任何公共网站。在那里看我的 cmets。

标签: jquery html html-table


【解决方案1】:

我只是使用 css:

<style>
table.collection {width:500px;border-collapse:collapse;}
table.collection tr {background-color:#fff; border-bottom: 1px #99b solid;}
table.collection tr:hover {background-color:#ffe;}
table.collection td {display:table-cell;border-bottom: 1px #99b solid; padding:0px;}
table.collection td a {text-decoration:none; display:block; padding:0px; height:100%;}
</style>
<table class="collection">
<tr>
    <td><a href="#">Linky1</a></td>
    <td><a href="#">Data1</a></td>
</tr>
<tr>
    <td><a href="#">Linky2</a></td>
    <td><a href="#">Data2</a></td>
</tr>
</table>

【讨论】:

  • 迄今为止最好的解决方案。浏览器仍将此视为链接,因此“在选项卡中打开”或“复制链接”将起作用。其他解决方案破坏了我的浏览器! JQuery 解决方案可以通过 DOM 操作自动执行上述操作。
  • 我在使用这个解决方案时遇到了问题。如果其中一个表格单元格包含足够的内容来换行,则相邻单元格的高度不会延伸到整个高度。您是否找到了解决这种情况的方法?
  • height:100% 在 FF 3.6、HTML5 doctype 中无法为我修复。
  • 这种方法的问题是你有很多链接,所以如果有人通过 Tab 键/屏幕阅读器等导航内容,他们将不得不跳过很多链接才能获得到下一个。
  • 为使可点击的东西成为实际链接而感到高兴。更好的是,使 href 在禁用 javascript 时可以工作。如果选项卡是一个问题,请将一行上除一个锚点之外的所有锚点设置为 tabIndex=-1
【解决方案2】:
$(document).ready(function(){
   $("tr").click(function(){
      /* personally I would throw a url attribute (<tr url="http://www.hunterconcepts.com">) on the tr and pull it off on click */
      window.location = $(this).attr("url");

   });
});

【讨论】:

  • 好的,这很好,但是如何让它在新窗口中打开呢?好像它会是一个 ...也是这种方式(仅当您单击 时,它才会通过引用字符串 ... 使用 js onclick - 不。 ..
  • 当人们尝试点击tr 时,问题就来了,就像他们点击a 元素一样(即shift+click 或cmd+click 来打开新标签等)
  • href 不是更好的属性吗?
【解决方案3】:

为 tr 元素注册一个 onclick 事件处理程序。像这样使用 jQuery:

$("tr").bind("click", function(){ 
  window.location = 'http://www.example.com/'; 
});

【讨论】:

  • 这不是将您链接到每行点击的相同位置吗?
  • 您可以在每个 tr 中使用隐藏的输入来存储 url。
  • 这确实会绑定相同的链接到每一行,根据你的需要修改..
【解决方案4】:

如果您不介意用通用元素替换表格,则不需要 jQuery:

<style>
    .table {
        border-collapse: collapse;
        border-spacing: 0;
        display: table;
    }
    .tr {
        display: table-row;
    }
    .td {
        display: table-cell;
    }

</style>

<section class="table">
    <a class="tr" href="#">
        <div class="td">
            A
        </div>
        <div class="td">
            B
        </div>
        <div class="td">
            C
        </div>
    </a>
</section>

【讨论】:

  • 在此解决方案中,您不会保留通过使用 table、tr 和 td 标签获得的语义含义。
【解决方案5】:
<td>
    <a href="/whatevs/whatevs">
        <div class="tdStreacher"> linkName
        </div>
    </a>
</td>

.tdStreacher{
    height: 100%;
    width: 100%;
    padding: 3px;
}

这样,每个单元格的所有区域都将充当链接,因此,整行都充当链接。

【讨论】:

  • 没有看到被接受的解决方案吧?不适用于 1999/xhtml,因为 height:100% 不一定跨越整个单元格。
【解决方案6】:

这是一个基于 Nick 解决方案的 jQuery 插件。

(function($) {
  $.fn.linkWholeRows = function() {

    // for each object
    return this.each(function() {

      // for each row
      $(this).find('tbody tr').each(function() {
        // get the first link's href
        var href = $(this).find('td > a').attr('href');
        // if none found then
        if (href === undefined) {
          return true; // continue
        }

        // wrap all cells with links that do not already have a link
        $(this).children().not(':has(a)').each(function() {
          $(this).contents().wrapAll('<a href="' + href + '" />');
        });

        // apply the row's height to all links
        // in case that the cells' content have different heights
        var height = $(this).children().css('height');
        $(this).find('td > a').each(function() {
          $(this).css('height', height);
          // do not forget to apply display:block to the links
          // via css to make it work properly
        });
      }); // each row

    }); // each object

  };
})(jQuery);

期望行被包裹在 tbody 中。高度是明确设置的,因为尼克的原始解决方案不适用于我在具有不同高度的相邻单元格上。 确保将 a 元素设置为块。如果要应用填充,请将其应用到 a 元素而不是表格单元格:

a {
  display: block;
  padding: 0.25em 0.5em;
}
tbody td { padding: 0; }

只需调用

$('#your-table').linkWholeRows();

希望对您有所帮助。干杯, 理查德

【讨论】:

    猜你喜欢
    • 2010-11-30
    • 2013-09-09
    • 1970-01-01
    • 2020-02-06
    • 2019-10-18
    • 2016-03-23
    • 2011-12-14
    • 1970-01-01
    相关资源
    最近更新 更多