表格周围的锚标记在 IE 6、7 和 IE 中不可单击8
问题:当用锚标记包围表格时,表格及其中的所有内容在 IE 6、7 和 IE 中都不可单击。 8. 假设我无法用 div 替换表格,如何解决这个问题?
示例代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US">
<head>
<title>Test</title>
</head>
<body>
<a href="http://www.google.com">
<table height="35">
<tr>
<td>I'm a link in a table, bet you can'tclick me!</td>
</tr>
</table>
</a>
</body>
</html>
The problem: when surrounding a table with an anchor tag, the table and everything within is not clickable in IE 6, 7 & 8. How do I solve this issue assuming I can't replace the table with divs?
Sample code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US">
<head>
<title>Test</title>
</head>
<body>
<a href="http://www.google.com">
<table height="35">
<tr>
<td>I'm a link in a table, bet you can'tclick me!</td>
</tr>
</table>
</a>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
您可以在表格上添加 JavaScript onclick 事件处理程序来执行与链接相同的操作。
编辑:删除了最初的建议,因为它在其他浏览器中表现不佳。
You can add a JavaScript onclick event handler on the table to do the same thing as the link.
Edit: Removed initial suggestion since it behaved badly in other browsers.
锚标记内不能有表格,因为表格是块标记,锚标记是内联标记。块标签不会进入内联标签内,因此代码无效。用 div 元素替换表格也不起作用,因为它们也是块元素。
该标准指定了有效代码应如何工作,但没有指定无效代码应如何工作。不同的浏览器有不同的方法来充分利用这种情况。在这种情况下,浏览器的一种替代方案是将锚点移动到表格内,另一种替代方案是将表格移出锚点。在某些情况下,这两种方法都会给出所需的结果,但在其他情况下则不然。
可靠地将块元素放入锚点内的唯一方法是使用默认为 inlinde 元素的元素,并使用 CSS 将两个元素转换为块元素:
代码有效,因为默认情况下两个元素都是内联元素,它在应用样式后起作用,因为块元素可以位于另一个块元素内部。
You can't have a table inside an anchor tag, as the table is a block tag and the anchor is an inline tag. Block tags don't go inside inline tags, so the code is invalid. Replacing the table with div elements doesn't work either, as they also are block elements.
The standards specifies how valid code should work, but not how invalid code should work. Different browsers have different methods of making the best of the situation. One alternative for the browser in this case is to move the anchor inside the table, another alternative is to move the table out of the anchor. Either method will give the desired result in some situations but not others.
The only way to reliably put a block element inside an anchor, is to use an element that is an inlinde element by default, and use CSS to turn both elements into block elements:
The code is valid as both elements are inline elements by default, and it works after the style is applied as a block element can be inside another block element.
为什么不这样做呢?
Why not do this?
这是一个替代解决方案:
Here is an alternative solution:
我今天也遇到了这个问题并寻找解决方案,但找不到有效的解决方案。所以我想出了这个小的 jquery 解决方案。您可以根据需要修改它。希望这能给那些遇到 IE7 问题的人提供一些想法。
I too got this problem today and searched for a fix, but couldn't find a working solution for it. So I just came up with this small jquery solution. You can modify it as you want. Hope this will give an idea to those of you struggling with this IE7 issue.
我设法找到了一个解决方案,它并不完美,但它有效:
简化的 CSS:
简化的 HTML:
测试页面
另一种方法是在 IE9 上的所有链接上使用 JavaScript 事件委托,如下所示:
I've managed to find a solution for this, it's not perfect but it works:
Simplified CSS:
Simplified HTML:
Test page
Another way is using JavaScript event delegation on all links on IE9 like below:
如果您疯狂到支持 IE 7、8 和 9,您应该知道在这些虚拟浏览器中,您不能在 A 标记内包含 TABLE。
即使在将锚点显示为块元素的情况下也是如此。但是,您可以将 div 放置在锚标记内,这对我来说更奇怪。
这个问题没有其他解决方案。
If you are crazy enough to support IE 7, 8 and 9 at all, you should know that you cannot have a TABLE inside an A tag in these dummy browsers.
This is true even in the case where you make the anchor display like a block element. You can however place a div inside an anchor tag, which for me is even more bizarre.
There are NO other solutions to this problem.
您可以将 href 属性直接添加到表中,而不是将表包装在锚标记中。
使用点击表格导航到相应的网址
You can add href attribute directly to table insted of wrapping table in anchor tag.
use on click on table for navigating to corrosponding url
如果你想这样做:
首先,你必须制作一个适用于所有浏览器而无需 JavaScript 的通用代码,例如:
现在,当你单击表格内的项目而不是整个表格时,它可以工作(但有一点工作!)。然后,您必须应用最终修复 JavaScript 代码,以防浏览器使用 jQuery 启用 JS,如下所示
:有效,如果浏览器没有启用 JS,则可以单击内部
If you want to do this:
First, you have to make an universal code that's work in all browsers without JavaScript like:
Now it works when you click items inside the table but no the entire table(but a little works!). Then, you have to apply a final fix JavaScript code in case browser has enable JS using jQuery like this:
And now it entirely works, in case browser doesn't have enable JS, works clicking inside