将类似 href 的属性应用于非 属性。元素

我一直在开发一个页面,其中有多个条目包含在不同的

中。每一个都只是一个链接到页面的标题、一张图像和一个简短的描述。然而,描述可以包含任意标签,包括标签。

由于这些非常简单,并且实际链接不是那么大,因此我将其设置为单击

将调用 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.

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

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

发布评论

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

评论(2

标点 2024-08-31 12:48:02

不完全是,不。尽管值得一读Eric Meyer 对此的想法。另外,看来 HTML51 包含任何元素成为链接的能力, 因此,如果可能的话,可能值得使用该 doctype 而不是 xhtml。

还值得补充的是,html 5 确实允许 元素包围块级元素,请参阅:http://www.brucelawson.co.uk/2008 /any-element-linking-in-html-5/,取自链接页面的示例:

而不是:

<h3><a href="story.htm">Bruce Lawson as Obama's running mate!</a></h3>
<a href="story.htm"><img src="bruce.jpg" alt="lovegod" /> </a>
<p><a href="story.htm">In answer to McCain's appointment of MILF, Sarah Palin, Obama hires DILF, Bruce Lawson, as his running mate. Read more!</a></p>

您可以说:

<a href="story.htm">
<h3>Bruce Lawson as Obama's running mate!</h3>
<img src="bruce.jpg" alt="lovegod" />
<p>In answer to McCain's appointment of MILF, Sarah Palin, Obama hires DILF, Bruce Lawson, as his running mate. Read more!</p>
</a>

更新以提及可能的不准确

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:

<h3><a href="story.htm">Bruce Lawson as Obama's running mate!</a></h3>
<a href="story.htm"><img src="bruce.jpg" alt="lovegod" /> </a>
<p><a href="story.htm">In answer to McCain's appointment of MILF, Sarah Palin, Obama hires DILF, Bruce Lawson, as his running mate. Read more!</a></p>

you can say:

<a href="story.htm">
<h3>Bruce Lawson as Obama's running mate!</h3>
<img src="bruce.jpg" alt="lovegod" />
<p>In answer to McCain's appointment of MILF, Sarah Palin, Obama hires DILF, Bruce Lawson, as his running mate. Read more!</p>
</a>

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... =/

放赐 2024-08-31 12:48:02

如果您希望链接覆盖整个 div,一个想法是创建一个空的 标记作为第一个子标记:

<div class="covered-div">
  <a class="cover-link" href="/my-link"></a>
  <!-- other content as usual -->
</div>
div.covered-div {
  position: relative;
}

a.cover-link {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
}

这在使用 < 时尤其有效code>

请注意,将其作为第一个子项意味着它将使文本内的其他链接或按钮无法通过单击访问。如果您希望它们可点击,那么您必须将其设为最后一个子项。

If you want a link to cover an entire div, an idea would be to create an empty <a> tag as the first child:

<div class="covered-div">
  <a class="cover-link" href="/my-link"></a>
  <!-- other content as usual -->
</div>
div.covered-div {
  position: relative;
}

a.cover-link {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
}

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.

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