简体   繁体   English

可以<span>制作成可点击的链接吗?</span>

[英]Can a <span> be made into a clickable link?

To make a span into a clickable link.将跨度变成可点击的链接。

I have made a span that contains only a background image (as part of a Gilder/Levin image replacement technique) into a clickable link, and it seems to work fine -- but, so far, that is only on my own desktop computer, and on Chrome, Opera, and IE 11.我制作了一个仅包含背景图像(作为 Gilder/Levin 图像替换技术的一部分)的跨度到一个可点击的链接中,它似乎工作正常——但是,到目前为止,这仅在我自己的台式计算机上,在 Chrome、Opera 和 IE 11 上。

Is this viable?这是可行的吗?

<div id="logo">
  <a href="[absolute url]"> 
    <span></span>
  </a>
  <h1>page name</h1>
</div>

It works on my computer, with Chrome, IE11 and Opera.它可以在我的电脑上使用 Chrome、IE11 和 Opera。 Will it work universally?它会普遍适用吗?

While it might look okay in most browsers, you're using the <a> element incorrectly, as what goes inside it should be a meaningful label.虽然它在大多数浏览器中看起来不错,但您错误地使用了<a>元素,因为它里面的内容应该是一个有意义的标签。 The proper thing to do would be to wrap the entire <h1> in the link, or to put the <a> within the <h1> (both are valid HTML5).正确的做法是将整个<h1>包裹在链接中,或者将<a>放在<h1> (两者都是有效的 HTML5)。

<a href="[absolute url]"> 
  <span></span> <h1>page name</h1>
</a>

But judging from your comments, it's probably too early for you to start worrying about image replacement techniques an web semantics when you're still figuring the syntax out.但是从您的评论来看,当您仍在弄清楚语法时,现在开始担心图像替换技术和网络语义可能为时过早。


What's the point of image replacement techniques and why using an empty <a> tag is bad?图像替换技术的意义何在?为什么使用空的<a>标签不好?

The Gilder/Levin image replacement technique involves adding non-semantic elements to a page (such as <span> elements) and using CSS to replace them with icons, so that these elements are ignored by screen readers. Gilder/Levin 图像替换技术包括向页面添加非语义元素(例如<span>元素)并使用 CSS 将它们替换为图标,以便屏幕阅读器忽略这些元素。 After all, an icon next to a menu button might make the button more visible for someone who can see, but the icon becomes redundant when you're blind and are using a screen reader which will read the text of the button out loud anyway.毕竟,菜单按钮旁边的图标可能会使可以看到的人更容易看到该按钮,但是当您失明并使用屏幕阅读器时,该图标就会变得多余,无论如何都会大声朗读按钮的文本。 This also might make your website easier to parse by search engines.这也可能使您的网站更容易被搜索引擎解析。

However, in the original code, you didn't put any label on the link (actual text between the <a> and </a> ), therefore making it especially confusing for screen readers and robots to know what this link is supposed to be.但是,在原始代码中,您没有在链接上放置任何标签( <a></a>之间的实际文本),因此使屏幕阅读器和机器人特别难以知道此链接应该做什么是。 The entire title should be within the <a> element in this case, allowing the whole line to be clicked to follow the link.在这种情况下,整个标题应该在<a>元素内,允许单击整行以跟随链接。 It's definitely not a good practice to use an empty <a> element, and the fact that there is a <span> within it changes nothing.使用空的<a>元素绝对不是一个好习惯,而且其中有一个<span>的事实不会改变任何东西。

And since the idea of leaving an <a> element is semantically absurd, I haven't found any reliable place documenting the behavior of such an element across browsers.而且由于离开<a>元素的想法在语义上是荒谬的,我还没有找到任何可靠的地方来记录这种元素跨浏览器的行为。

wasn't pretty sure what you are asking for:: or trying to achieve.不太确定你在要求什么:或试图实现。
3. wrap span in a href tag. 3. 将 span 包裹在 href 标签中。
2. span onclick() function with javascript 2. 使用 javascript 扩展 onclick() 函数
1. span:hover with css. 1.跨度:用css悬停。

<div id="logo">
   <a href="[absolute url]">
      <span>this span is now like link text.</span>
   </a>
   <h1>page name</h1>
</div>


<div id="logo">
     <span onclick="myFunction()">this span is now like link text.</span>
   <h1>page name</h1>
</div>

<style>
span:hover{color:red;}
span:active {color:green}
</style>

The css one isn't really click stuff. css 不是真正的点击的东西。

Yes, it's a reliable way to put <span> or <img> (or any element you want to be a link) in a <a> tag.是的,将<span><img> (或任何您想作为链接的元素)放在<a>标签中是一种可靠的方法。

click here for Definition and Usage 单击此处了解定义和用法

The tag defines a hyperlink, which is used to link from one page to another.该标签定义了一个超链接,用于从一个页面链接到另一个页面。

The most important attribute of the element is the href attribute, which indicates the link's destination.该元素最重要的属性是 href 属性,它表示链接的目的地。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM