如何将链接扩展到整个单元格?

发布于 2025-01-02 07:31:47 字数 349 浏览 3 评论 0原文

我有一个表,其中包含可以单击以编辑行的链接(锚点)。 我希望将这些链接拉伸到包含单元格的完整宽度和高度。我已经将它们设置为 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:

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

公布 2025-01-09 07:31:47

在块元素上设置任意大的负边距和相等的填充,并在父元素上隐藏溢出。

td {
    overflow: hidden;
}
td a {
    display: block;
    margin: -10em;
    padding: 10em;
}

http://jsfiddle.net/53Ptm/43/

Set an arbitrarily large negative margin and equal padding on the block element and overflow hidden on the parent.

td {
    overflow: hidden;
}
td a {
    display: block;
    margin: -10em;
    padding: 10em;
}

http://jsfiddle.net/53Ptm/43/

枕头说它不想醒 2025-01-09 07:31:47

我不认为它可以只使用 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.

未央 2025-01-09 07:31:47

您可以在每个单元格上使用一点 JavaScript 来修复此问题,这样当单击单元格时 JavaScript 会将用户发送到一个 URL。您需要对每个单元格使用这行代码 < td> < /td>

<td bgcolor="lightgray" onClick="document.location.href='http://www.URLHERE.com/';" style="cursor:pointer;"><a>c 1</a></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 >

<td bgcolor="lightgray" onClick="document.location.href='http://www.URLHERE.com/';" style="cursor:pointer;"><a>c 1</a></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.

温柔嚣张 2025-01-09 07:31:47

使用 jQuery 您可以将 click 事件侦听器附加到整个 页面。 元素。

假设变量 redirectLink 是在脚本的前面定义的。
例如:
var redirectLink = "http://msmvps.com/blogs/jon_skeet/";
但这可以是您选择的任何其他很棒的 URL ;)

$(".cellLink").click(function(){
  // execute the redirect here 
  top.location.href = redirectLink;
});

$(".cellLinks")jQuery 选择器,其中包含带有 cellLink 类的所有元素。示例:
c 1

cellLink 只是我发明的一个名称 - 您可以将其命名为任何对您来说听起来合乎逻辑的名称。

如果您在整行上放置相同的 cellLink 类,则可以在整行上实现相同的效果:

<tr class="cellLink">
  <td>...</td>
  ...
</tr>

在这种情况下,将其称为更通用的名称可能是更好的主意。

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 ;)

$(".cellLink").click(function(){
  // execute the redirect here 
  top.location.href = redirectLink;
});

The $(".cellLinks") is a jQuery Selector that includes all elements with the cellLink 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 :

<tr class="cellLink">
  <td>...</td>
  ...
</tr>

In this case it might be a better idea to call it something more generic.

深海少女心 2025-01-09 07:31:47

我会选择 Jannis M答案,但将其修改为使用每行的高度,如下所示:

$(document).ready(function(){    
    $('table tr').each(function(){
        var $row = $(this);
        var height = $row.height();
        $row.find('a').css('height', height).append(' ');  
    });       
});

另外,我添加了一个   因为空链接(不包含文本节点)可以不被设计风格(?)。

请参阅我更新的fiddle

更新:
或者参见 Gaurav Saxenaanswer 适用于现代浏览器的纯 CSS 解决方案。

I'm gonna go with Jannis M's answer, but modified it to use each row's height like this:

$(document).ready(function(){    
    $('table tr').each(function(){
        var $row = $(this);
        var height = $row.height();
        $row.find('a').css('height', height).append(' ');  
    });       
});

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文