我一直在开发一个页面,其中有多个条目包含在不同的
中。每一个都只是一个链接到页面的标题、一张图像和一个简短的描述。然而,描述可以包含任意标签,包括
标签。
由于这些非常简单,并且实际链接不是那么大,因此我将其设置为单击
将调用
location.href = (link URL)< /代码>。然而,这是一件非常可悲的事情,因为它对浏览器不友好:例如,在 Google Chrome 下,中键单击其中一个
不会在新窗口中打开链接选项卡。
考虑到您不应该嵌套
标签,是否可以使 XHTML 中的任何元素表现得像链接而不诉诸 Javascript?
我使用的是 XHTML 1.1,以正确的 MIME 类型发送,这是我必须遵守的唯一限制。
I've been working on a page where there are several entries contained in different <div>
s. Each is only a title linked to a page, an image and a short description. However, the description may contain arbitrary tags, including <a>
tags.
Since these are pretty straightforward and the actual link isn't that big, I've made it so a click on the <div>
will call location.href = (link URL)
. However, that's a pretty sad thing, because it's browser-unfriendly: for instance, under Google Chrome, a middle-click on one of said <div>
s won't open the link in a new tab.
Considering you shouldn't nest <a>
tags, is it possible to make any element in XHTML behave like a link without resorting to Javascript?
I'm using XHTML 1.1, sent with the proper MIME type, and that's the only restriction I'm bound to.
发布评论
评论(2)
不完全是,不。尽管值得一读Eric Meyer 对此的想法。另外,
看来 HTML51 包含任何元素成为链接的能力,因此,如果可能的话,可能值得使用该 doctype 而不是 xhtml。还值得补充的是,html 5 确实允许
元素包围块级元素,请参阅:http://www.brucelawson.co.uk/2008 /any-element-linking-in-html-5/,取自链接页面的示例:
而不是:
您可以说:
更新以提及可能的不准确
1:我可能误解了我链接到的文档的一部分,试图为我的主张“...似乎 HTML5...任何元素都可以成为链接”(在 W3C 的 html 5 概述)它似乎不存在。当我看到迈耶的提议包含这种可能性时,我想我受到了过度的鼓励。
我太轻信了,太天真了……=/
Not really, no. Though it's worth reading Eric Meyer's thoughts on this. Also,
it appears that HTML51 includes the capacity for any element to become a link,so it might be worth using that doctype instead of xhtml, if possible.It's worth also adding that html 5 does allow for an
<a>
element to enclose block-level elements, see: http://www.brucelawson.co.uk/2008/any-element-linking-in-html-5/, example taken from the linked page:Instead of:
you can say:
Updated to mention possible inaccuracy
1: I may have misinterpreted part of the document to which I linked, having tried to find support for my claim that '...appears that HTML5...any element to become a link' (in the W3C's html 5 overview) it doesn't seem to be there. I think I was over-encouraged when I saw Meyer's proposal to include that possibility.
I'm too gullible, and naive... =/
如果您希望链接覆盖整个
div
,一个想法是创建一个空的标记作为第一个子标记:
这在使用 < 时尤其有效code>
创建块部分或幻灯片,并且您希望整个幻灯片成为一个链接(而不仅仅是幻灯片上的文本)。对于
- 块。
,用
包装它是无效的,因此您必须将封面链接放在项目内并使用CSS 将其扩展到整个
请注意,将其作为第一个子项意味着它将使文本内的其他链接或按钮无法通过单击访问。如果您希望它们可点击,那么您必须将其设为最后一个子项。
If you want a link to cover an entire
div
, an idea would be to create an empty<a>
tag as the first child:This works especially great when using
<ul>
to create block sections or slideshows and you want the whole slide to be a link (instead of simply the text on the slide). In the case of an<li>
it's not valid to wrap it with an<a>
so you'd have to put the cover link inside the item and use CSS to expand it over the entire<li>
block.Do note that having it as the first child means it will make other links or buttons inside the text unreachable by clicks. If you want them to be clickable, then you'd have to make it the last child instead.