如何将链接扩展到整个单元格?
我有一个表,其中包含可以单击以编辑行的链接(锚点)。 我希望将这些链接拉伸到包含单元格的完整宽度和高度。我已经将它们设置为 display: block;
因此它们具有完整宽度:
问题是,我无法使用 CSS 将它们设置为全高。请参阅我的示例小提琴: http://jsfiddle.net/timbuethe/53Ptm/2/ 。
I have a table that includes links (anchors) that can be clicked to edit the row. I want these links to be stretched to the full width and height of the containing cell. I already set them to display: block;
so they have the full width:
The problem is, I have trouble getting them to full height using CSS. See my example fiddle here: http://jsfiddle.net/timbuethe/53Ptm/2/.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在块元素上设置任意大的负边距和相等的填充,并在父元素上隐藏溢出。
http://jsfiddle.net/53Ptm/43/
Set an arbitrarily large negative margin and equal padding on the block element and overflow hidden on the parent.
http://jsfiddle.net/53Ptm/43/
我不认为它可以只使用 CSS 来制作,因为 高度属性始终引用元素内容,而不是父元素高度。
再次检查 Fiddle 并了解它是如何用简单的 JavaScript 制作的。它获取父元素的高度并将其保存在一个变量中,然后将其附加到您的锚元素。
I don't think that it can be made using only CSS, because the height property is always refering to the elements content, not the parent elements height.
Check the Fiddle again and see how it is made with a simple javascript. It fetched the parents elements height and saves it in a variable, which is then appended ass style rule to your anchor element.
您可以在每个单元格上使用一点 JavaScript 来修复此问题,这样当单击单元格时 JavaScript 会将用户发送到一个 URL。您需要对每个单元格使用这行代码 < td> < /td>
我保留了< a> < /一>标记到位,以防万一用户没有启用 javascript,但单元格的背景将保持不变,并且使用这段 javascript 应该覆盖大多数用户。
You can fix this with a little javascript on each cell so the javascript will send the user to a url when the cell is clicked on. You'll want to use this line of code for each cell < td > < / td >
I've kept the < a > < / a > tag in place just in case the user doesn't have javascript enabled, but the background of the cell will stay intact and using this piece of javascript should cover most users.
使用 jQuery 您可以将
click
事件侦听器附加到整个页面。
元素。假设变量
redirectLink
是在脚本的前面定义的。例如:
var redirectLink = "http://msmvps.com/blogs/jon_skeet/";
但这可以是您选择的任何其他很棒的 URL ;)
$(".cellLinks")
是 jQuery 选择器,其中包含带有cellLink
类的所有元素。示例:c 1
cellLink
只是我发明的一个名称 - 您可以将其命名为任何对您来说听起来合乎逻辑的名称。如果您在整行上放置相同的
cellLink
类,则可以在整行上实现相同的效果:在这种情况下,将其称为更通用的名称可能是更好的主意。
Using jQuery you could attach a
click
event listener to the entire<td>
element.Assuming that the variable
redirectLink
was defined earlier in the script.for example :
var redirectLink = "http://msmvps.com/blogs/jon_skeet/";
but this could be any other awesome URL u choose ;)
The
$(".cellLinks")
is a jQuery Selector that includes all elements with thecellLink
class on them. example:<td class="cellLink">c 1</td>
cellLink
is just a name that I invented - you can call it whatever name sounds logical to you.The same could be achieved on an entire row if you place the same
cellLink
class on it :In this case it might be a better idea to call it something more generic.
我会选择 Jannis M 的 答案,但将其修改为使用每行的高度,如下所示:
另外,我添加了一个
因为空链接(不包含文本节点)可以不被设计风格(?)。
请参阅我更新的fiddle。
更新:
或者参见 Gaurav Saxena 的 answer 适用于现代浏览器的纯 CSS 解决方案。
I'm gonna go with Jannis M's answer, but modified it to use each row's height like this:
Additional, I added a
since empty links (not containing text nodes) can not be styled(?).
See my updated fiddle.
UPDATE:
Alternatively see Gaurav Saxena's answer for a CSS only solution that works in modern browsers.