我知道可以使用 CSS 来链接整个表格单元格。
.tableClass td a{
display: block;
}
有没有办法将链接应用到整个表格行?
我同意Matti的观点。使用一些简单的javascript可以轻松地完成。一个快速的jquery示例可能如下所示:
<tr>
<td><a href="http://www.example.com/">example</a></td>
<td>another cell</td>
<td>one more</td>
</tr>
和
$('tr').click( function() {
window.location = $(this).find('a').attr('href');
}).hover( function() {
$(this).toggleClass('hover');
});
然后在你的CSS中
tr.hover {
cursor: pointer;
/* whatever other hover styles you want */
}
::before
伪元素。这样,您就不必处理JavaScript或为每个单元格创建链接。使用以下table
结构。<table>
<tr>
<td><a href="http://domain.tld" class="rowlink">Cell</a></td>
<td>Cell</td>
<td>Cell</td>
</tr>
</table>
.rowlink
)上使用::before
,创建一个横跨整个表格宽度的块元素。table {
position: relative;
}
.rowlink::before {
content: "";
display: block;
position: absolute;
left: 0;
width: 100%;
height: 1.5em; /* don't forget to set the height! */
}
在演示中,::before
以红色突出显示,这样你就可以看到它的作用了。
padding-top
的td
进行调整? - eirikir.rowlink::before
中添加一个负的 margin-top
,值等于 td
的 padding-top
对我有用。 - jzzocctr
,使用 delegate
比使用 live
更高效。 - Yi Jianglive
只是在文档级别上附加一个事件处理程序,然后处理所有事情。现在,如果你有很多处理程序,live
可能会变得低效,因为它们将为该类型的所有事件处理程序进行处理,但元素的数量不应该有任何影响。 - Matti Virkkunendelegate
附加到table
元素上,事件只需要通过一个元素冒泡即可。 - Yi Jiang示例: http://xxjjnn.com/linktablerow.html
链接整行:
<table>
<tr onclick="location.href='SomeWherrrreOverTheWebsiiiite.html'">**
<td> ...content... </td>
<td> ...content... </td>
...
</tr>
</table>
如果您想对整行进行鼠标悬停高亮显示,则:
<table class="nogap">
<tr class="lovelyrow" onclick="location.href='SomeWherrrreOverTheWebsiiiite.html'">**
...
</tr>
</table>
使用类似以下的CSS代码,可以消除表格单元格之间的间隙并在鼠标悬停时改变背景色:
```css table { border-collapse: collapse; }tr.lovelyrow{
background-color: hsl(0,0%,90%);
}
tr.lovelyrow:hover{
background-color: hsl(0,0%,40%);
cursor: pointer;
}
table.nogap{
border-collapse: collapse;
}
如果你正在使用Rails 3.0.9,那么你可能会发现这个示例代码很有用:
大海有很多鱼,鱼有很多鳞片,在这里是app/view/fish/index.erb的一部分摘录
<table>
<% @fishies.each do |fish| %>
<tr onclick="location.href='<%= sea_fish_scales_path(@sea, fish) %>'">
<td><%= fish.title %></td>
</tr>
<% end %>
</table>
@fishies和@sea在app/controllers/seas_controller.rb中定义。
:hover
可以省略,或者你甚至可以内联样式,例如 <tr style="cursor:pointer" onclick="location.href='url'">
。 - Aaron Blenkush还要看你是否需要使用表格元素。你可以使用CSS模仿表格,将A元素作为行。
<div class="table" style="width:100%;">
<a href="#" class="tr">
<span class="td">
cell 1
</span>
<span class="td">
cell 2
</span>
</a>
</div>
样式表:
.table{display:table;}
.tr{display:table-row;}
.td{display:table-cell;}
.tr:hover{background-color:#ccc;}
<a>
不能同时具有 role="row"
和 href
。w3c 验证器会报错。 - user8977154我觉得最简单的解决方案是不使用JavaScript,只是把链接放在每个单元格中(前提是单元格之间没有很大的沟壑或者你不认为需要边框线)。你可以如下设置CSS:
.tableClass td a{
display: block;
}
然后为每个单元格添加一个链接:
<table class="tableClass">
<tr>
<td><a href="#link">Link name</a></td>
<td><a href="#link">Link description</a></td>
<td><a href="#link">Link somthing else</a></td>
</tr>
</table>
枯燥但干净。
onclick
功能,该功能位于<tr>
元素中,并为tr
元素在CSS中定义鼠标hover
,使鼠标指针成为Web中典型的点击手形:
在表格中:
<tr onclick="location.href='http://www.google.com'">
<td>blah</td>
<td>blah</td>
<td><strong>Text</strong></td>
</tr>
tr:hover {
cursor: pointer;
}
<tr onclick="location.href='http://www.mywebsite.com'" style="cursor: pointer">
<td>...</td>
<td>...</td>
</tr>
当鼠标指针悬停在元素上方时,光标CSS属性设置要显示的光标类型(如果有)。
内联css定义了对于该元素来说,光标将被格式化为pointer,因此您不需要“hover”。