如何在HTML中不使用JavaScript或jQuery使整个表格单元格超链接化?
我尝试将href放置在td标签本身中,但至少在chrome 18中它不起作用。
<td href='http://www.m-w.com/dictionary/' style="cursor:pointer">
试一下这个:
HTML:
<table width="200" border="1" class="table">
<tr>
<td><a href="#"> </a></td>
<td> </td>
<td> </td>
</tr>
</table>
CSS:
.table a
{
display:block;
text-decoration:none;
}
我希望它能够正常工作。
试试这种方式:
<td><a href="..." style="display:block;"> </a></td>
通过onclick函数和javascript链接很容易实现:
<td onclick="location.href='你的页面.html'">去你的页面</td>
<td>
元素中的所有空间。这是唯一解决问题的答案! - jfdoming问题:
(用户:Kamal) 这是一种好方法,但你忘了垂直对齐的问题!使用这种方法,我们无法将链接放在 TD 元素的中心位置!即使使用 vertical-align:middle;
(用户:Christ) 你的答案是最好的答案,因为没有任何对齐问题,并且今天 JavaScript 对于每个人都是必要的... 它无处不在,甚至在旧智能手机上也可以使用... 而且它默认启用...
我的建议来完善 (用户:Christ) 的答案:
HTML:
<td style="cursor:pointer" onclick="location.href='mylink.html'"><a class="LN1 LN2 LN3 LN4 LN5" href="mylink.html" target="_top">link</a></td>
a.LN1 {
font-style:normal;
font-weight:bold;
font-size:1.0em;
}
a.LN2:link {
color:#A4DCF5;
text-decoration:none;
}
a.LN3:visited {
color:#A4DCF5;
text-decoration:none;
}
a.LN4:hover {
color:#A4DCF5;
text-decoration:none;
}
a.LN5:active {
color:#A4DCF5;
text-decoration:none;
}
这是我的解决方案:
<td>
<a href="/yourURL"></a>
<div class="item-container">
<img class="icon" src="/iconURL" />
<p class="name">
SomeText
</p>
</div>
</td>
td {
padding: 1%;
vertical-align: bottom;
position:relative;
a {
height: 100%;
display: block;
position: absolute;
top:0;
bottom:0;
right:0;
left:0;
}
.item-container {
/*...*/
}
}
像这样,您仍然可以从一些表格单元格属性中受益,例如 vertical-align
。(在Chrome上测试过)
HTML:
<table>
<tr>
<a href="...">Cell 1</a>
<td>Cell 2</td>
</tr>
</table>
tr > a {
display: table-cell;
}
。CSS可能需要一些额外的调整才能使其正常工作,例如vertical-align:center
,但这没问题。 - Magnus
。CSS可能需要一些额外的调整才能实现相同效果,比如 vertical-align:center
,但这没问题。 - Magnus我曾经看到过人们在尝试构建日历时遇到过这种情况。你想要链接单元格,但又不想干扰其中的其他内容,尝试一下这个方法,它可能会解决你的问题。
<tr>
<td onClick="location.href='http://www.stackoverflow.com';">
Cell content goes here
</td>
</tr>
我遇到了这个问题,使用了display block方法让a元素填充整个单元格。唯一的问题是当表格单元格有padding时,不是整个单元格都可以点击。
我使用CSS从包含锚点的表格单元格中删除了padding,并将相同的padding添加到锚点中。
所以最终我得到了这个:
table {
border-collapse: collapse;
}
td,
th {
border: 1px solid #ddd;
}
td,
th,
.anchor_cell a {
padding: 8px;
}
.anchor_cell {
padding: 0;
}
.anchor_cell a {
display: block;
}
<table>
<thead>
<tr>
<th>Name</th>
<th>ID</th>
</tr>
</thead>
<tbody>
<tr>
<td class="anchor_cell"><a href="/the_url">John Doe</a></td>
<td>1</td>
</tr>
<tr>
<td class="anchor_cell"><a href="/the_url">Jane Doe</a></td>
<td>1</td>
</tr>
</tbody>
</table>
<a href="https://www.foo.bar" target="_blank" style="color: white; font-weight: bolder; text-decoration: none;">
<table style="margin-left: auto; margin-right: auto;" align="center">
<tr>
<td style="padding: 20px; height: 60px;" bgcolor="#00b389">Go to Foo Bar</td>
</tr>
</table>
</a>
<a>
标签将一个单元格转化为超链接。例如,如果您想将表格中的第一个单元格变成指向Google网站的超链接,您可以这样编写HTML代码:
Google
其他单元格
在这个例子中,<a>
标签包裹在<td>
标签中,使得该单元格可以被点击并跳转到指定的网页。要更改链接的目标网址,请将href
属性更改为所需的URL。 - Usman