绝对定位的锚标记(无文本)在 IE 中不可点击

发布于 2024-11-27 16:06:29 字数 1277 浏览 0 评论 0原文

我有两个锚点绝对位于图像顶部,这些链接在其他浏览器(Chrome、FF、Safari)中可单击,但在 IE 中不可单击(到目前为止在 8 和 9 中进行了测试)

奇怪的是,如果我给链接指定一个background-color 它们是可点击的,但是如果background-color设置为transparent这就是我想要的)他们不再是可点击,我也尝试过设置 zoom:1 但没有运气。我猜 IE 中的 hasLayout 位在 IE 8/9 中消失了,所以猜测 zoom 对于此类问题现在并不重要。

有什么想法可以让这些链接以透明背景显示在 IE 8/9 中吗?

这是我一直在使用的小提琴: jsFiddle 示例

我有以下 HTML 布局:

<div id="content">
    <img src="http://placehold.it/724x300" width="724" height="300" alt="woot" />

    <div id="countdown"></div>

    <a id="link1" href="http://www.stackoverflow.com" title="link1"></a>
    <a id="link2" href="http://www.stackoverflow.com" title="link2"></a>
</div>

和 CSS:

body {text-align:center;}
#content {position:relative; width:724px; height:300px; margin:0 auto;}

#countdown {position:absolute; width:650px; height:110px; top:100px; left:30px; background-color:blue;}

#link1 {position:absolute; width:520px; height:35px; bottom:20px; left:0;}
#link2 {position:absolute; width:200px; height:35px; bottom:20px; right:0;}

I have two anchors positioned absolute on top of an image, the links are clickable in other browsers (Chrome, FF, Safari) but not in IE (tested in 8 & 9 so far)

The strange thing is if I give the links a background-color they are clickable, however if the background-color is set to transparent (which is what I want) they are no longer clickable, I've also tried setting zoom:1 but no luck. I guess the hasLayout bit in IE went away with IE 8/9 so guessing zoom doesn't matter now for this kind of issue.

Any ideas to make these links show up in IE 8/9 with a transparent bg?

Here's the fiddle I've been working with: jsFiddle example

I have the following HTML layout:

<div id="content">
    <img src="http://placehold.it/724x300" width="724" height="300" alt="woot" />

    <div id="countdown"></div>

    <a id="link1" href="http://www.stackoverflow.com" title="link1"></a>
    <a id="link2" href="http://www.stackoverflow.com" title="link2"></a>
</div>

and CSS:

body {text-align:center;}
#content {position:relative; width:724px; height:300px; margin:0 auto;}

#countdown {position:absolute; width:650px; height:110px; top:100px; left:30px; background-color:blue;}

#link1 {position:absolute; width:520px; height:35px; bottom:20px; left:0;}
#link2 {position:absolute; width:200px; height:35px; bottom:20px; right:0;}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

小…楫夜泊 2024-12-04 16:06:29

我以前也遇到过这个问题,而且它一直让我很恼火。我永远不知道为什么会发生这种情况,但我总是创建一个 1px x 1px 透明 PNG(或 GIF),并在背景声明中使用它,如下所示:

a { background: url("/path/to/image.png") 0 0 repeat; }

希望这有帮助。

PS - 不要用它指定任何实际的背景颜色。只需使用上面的示例就可以了。

除此之外,尝试将锚标记设置为显示为块,也许还可以在其中插入一个不间断的空格(因为它们目前是空的,这可能会导致 IE 崩溃):

a { display: block; background: url("/path/to/image.png") 0 0 repeat; }

<a href="#"> </a>

I have had this exact problem before and it has always annoyed the hell out of me. I'm never sure why it happens, but I always create a 1px by 1px transparent PNG (or GIF) and use that in your background declaration like so:

a { background: url("/path/to/image.png") 0 0 repeat; }

Hope this helps.

PS - Don't specify any actual background colour with this. Just use the example above and it should work.

In addition to this, try and set your anchor tags to display as block and perhaps also insert a non-breaking space in them (since they are empty at the moment and that might be tripping IE up):

a { display: block; background: url("/path/to/image.png") 0 0 repeat; }

<a href="#"> </a>
少女净妖师 2024-12-04 16:06:29

您可以使用提到的背景图像技巧来做到这一点。如果您不想为此使用透明图像,只需在图像 URL 中使用未知方案或 about:blank 即可。

a { background-image: url("iehack:///"); }

或者

a { background-image: url("about:blank"); }

这至少在 IE 8 + 9(我测试的唯一 IE)以及当前版本的 Firefox 和 Chrome 中有效。它仍然是有效的 CSS,不会导致额外的流量。当使用 jquery 时,第一个“hack”可能会在 Chrome(也可能是其他)中给出 JS 错误。由于 about:blank 文档的 MIME-Type 错误,后者仅(但肯定)在 Chrome 中给出 MIME-Type 警告。

You can do it using the mentioned background-image trick. When you do not want to use a transparent image for this, just use an unknown scheme or about:blank in the image URL.

a { background-image: url("iehack:///"); }

or

a { background-image: url("about:blank"); }

This works at least in IE 8 + 9 (the only IEs I tested) and in current versions of Firefox and Chrome. It is still valid CSS and causes no additional traffic. The first "hack" may give a JS error in Chrome (and maybe others) when using jquery. The latter only (but surely) gives a MIME-Type warning in Chrome due to the wrong MIME-Type of the about:blank document.

浅笑依然 2024-12-04 16:06:29

移植自我前段时间发表的评论。

稍长一点,但仍然没有流量,base 64 编码的透明 gif:

background-image: url('data:image/gif;base64,R0lGODlhAQABAPAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICR‌​AEAOw==')

这有其自身的优点/缺点,但可能很有用。另外:

background-color: rgba(0,0,0,0)

我最近用过这个

Transplanted from a comment I posted some time ago.

Little longer, but still no traffic, base 64 encoded transparent gif:

background-image: url('data:image/gif;base64,R0lGODlhAQABAPAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICR‌​AEAOw==')

This has its own pros/cons, but can be useful. Also:

background-color: rgba(0,0,0,0)

I've used this one more recently

厌味 2024-12-04 16:06:29

@tw16 提到invisible的评论让我想到了不透明度

将 IE 的 filter:alpha(opacity=0)background-color:#fff (或任何颜色)相结合似乎是一个很好的解决方案。在 IE 7-9 中进行了测试和工作(6 由于某种原因没有应用不透明度过滤器,但我不需要支持 6,所以这将工作)

1x1 图像解决方案是全局有效的,所以我要进行检查给他。

感谢您的回答。

@tw16's comment mentioning invisible got me thinking about opacity.

Combining IE's filter:alpha(opacity=0) with background-color:#fff (or any color) appears to be a good solution for this. Tested and works in IE 7-9 (6 isn't applying the opacity filter for some reason but I'm not required to support 6 so this will work)

The 1x1 image solution is globally effect so I'm going to give the check to him.

Thanks for the answers.

↘人皮目录ツ 2024-12-04 16:06:29

我也有同样的问题。我的工作解决方案是为适当的锚点添加边框。就像下面的例子一样。优点之一是,您不需要额外的图像。

a { border-right: 1px solid transparent }

I had the same problem. My working solution was to add a border to the appropriate anchor. Like the following example. One advantage is, you need no extra image.

a { border-right: 1px solid transparent }
我的痛♀有谁懂 2024-12-04 16:06:29

IE 有一个令人讨厌的习惯,当链接没有任何内容时,它不会使链接按预期工作。要解决此问题,请为每个链接指定一个   内容。

另一件可以尝试的事情是在链接上设置 display: block; 以使 IE 将它们作为块级元素而不是行内元素流动。如果您需要链接为空,这也可以提供帮助。

IE has a nasty habit of not making links work as expected when they don't have any content. To fix this, give each link a   for content.

Another thing to try is to set display: block; on the links to make IE flow them as block-level elements rather than as in-line elements. This can also help is you need the links to be empty.

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