【发布时间】:2011-06-21 18:06:32
【问题描述】:
我知道可以用 CSS 链接整个表格单元格。
.tableClass td a{
display: block;
}
有没有办法将链接应用到整个表格行?
【问题讨论】:
我知道可以用 CSS 链接整个表格单元格。
.tableClass td a{
display: block;
}
有没有办法将链接应用到整个表格行?
【问题讨论】:
很遗憾,没有。不是 HTML 和 CSS。您需要一个 a 元素来创建链接,并且您不能将整个表格行包装在一个中。
您可以获得的最接近的方法是链接每个表格单元格。就我个人而言,我只会链接一个单元格并使用 JavaScript 使其余部分可点击。为了清晰起见,最好至少有一个看起来像链接的单元格,加上下划线等等。
这是一个简单的 jQuery sn-p 让所有带有链接的表格行都可以点击(它会查找第一个链接并“点击”它)
$("table").on("click", "tr", function(e) {
if ($(e.target).is("a,input")) // anything else you don't want to trigger the click
return;
location.href = $(this).find("a").attr("href");
});
【讨论】:
tr,delegate 比使用live 效率更高
live 只是将一个事件处理程序附加到文档级别并处理所有事情。现在,如果您有很多 处理程序,live 可能会变得低效,因为它们基本上都会针对该类型的所有事件进行处理,但元素的数量应该没有区别。跨度>
我同意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 */
}
【讨论】:
示例: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 使用类似以下的内容,这将消除表格单元格之间的间隙并更改悬停时的背景:
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的sn-p
<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'">
我觉得最简单的解决方案是不使用 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>
无聊但干净。
【讨论】:
使用::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 在演示中以红色突出显示,因此您可以看到它在做什么。
【讨论】:
td 和padding-top 调整这个?
margin-top 添加到 .rowlink::before 等于 td 的 padding-top 对我有用。
还取决于您是否需要使用表格元素。您可以使用 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>
css:
.table{display:table;}
.tr{display:table-row;}
.td{display:table-cell;}
.tr:hover{background-color:#ccc;}
【讨论】:
要链接整行,你需要在你的行上定义onclick函数,即<tr>element,并在tr元素的CSS中定义一个鼠标hover,使鼠标指针指向一个典型的网页中的点击手:
在表中:
<tr onclick="location.href='http://www.google.com'">
<td>blah</td>
<td>blah</td>
<td><strong>Text</strong></td>
</tr>
在相关的 CSS 中:
tr:hover {
cursor: pointer;
}
【讨论】:
我认为这可能是最简单的解决方案:
<tr onclick="location.href='http://www.mywebsite.com'" style="cursor: pointer">
<td>...</td>
<td>...</td>
</tr>
cursor CSS 属性设置光标的类型,如果有的话,在什么时候显示 鼠标指针在一个元素上。
内联 css 定义 对于该元素,光标将被格式化为 pointer,因此您不需要“悬停”。
【讨论】: