简体   繁体   English

如何在 html 中的图标上显示简单的工具提示?

[英]How can I display simple tooltips on icons in html?

I am using ActiveScaffold in a Ruby on Rails app, and to save space in the table I have replaced the default "actions" text in the table (ie. "edit", "delete", "show") with icons using CSS.我在 Ruby on Rails 应用程序中使用 ActiveScaffold,为了节省表格中的空间,我使用 CSS 用图标替换了表格中的默认“操作”文本(即“编辑”、“删除”、“显示”)。 I have also added a couple of custom actions with action_link.add ("move" and "copy").我还使用 action_link.add(“移动”和“复制”)添加了几个自定义操作。

For clarity, I would like to have a tooltip pop up with the related action (ie. "edit", "copy") when I hover the mouse over the icon.为清楚起见,当我将鼠标悬停在图标上时,我希望弹出一个带有相关操作(即“编辑”、“复制”)的工具提示

I thought I could do this by adding a simple "alt" definition to the tag, but that doesn't appear to work.我以为我可以通过向标签添加一个简单的“alt”定义来做到这一点,但这似乎不起作用。

Can somebody point me in the right direction?有人可以指出我正确的方向吗?

The alt attribute is to be used as an alternative to the image, in the case of the image missing, or in a text only browser.在图像丢失的情况下,或者在纯文本浏览器中, alt属性将用作图像的替代。

IE got it wrong, when they made alt appear as a tooltip. IE 弄错了,当他们将alt显示为工具提示时。 It was never meant to be that.本来就不是这个意思。

The correct attribute for this is title , which of course doesn't do a tooltip in IE.正确的属性是title ,它当然不会在 IE 中做工具提示。

So, to do have a tooltip show up in both IE, and FireFox/Safari/Chrome/Opera, use both an alt attribute and a title attribute.因此,要在 IE 和 FireFox/Safari/Chrome/Opera 中都显示工具提示,请同时使用alt属性和title属性。

Just a minor point to add to this thread... there is no alt tag or title tag.只是要添加到此线程的一个小要点……没有 alt 标签或标题标签。 The alt attribute is for images, but all other elements on a page can have a title attribute, which is the best choice for cross browser compatibility. alt 属性用于图像,但页面上的所有其他元素都可以有一个 title 属性,这是跨浏览器兼容性的最佳选择。

<span title="Click here to edit the foo">
  Edit
</span>

You want a "title" tag.你想要一个“标题”标签。 I'm not sure if this is necessary anymore, but I usually add both alt and title tags to make sure all browsers display the tool tip the same.我不确定这是否有必要,但我通常会同时添加 alt 和标题标签,以确保所有浏览器都显示相同的工具提示。

Tooltips in HTML are the contents of the alt text for image tags, but if you're setting this using CSS you probably have a background:url(...); HTML 中的工具提示是图像标签的alt文本的内容,但如果您使用 CSS 设置它,您可能有一个background:url(...); style instead of an image.样式而不是图像。

在图像上使用alt并在链接上使用title

The alt property of an img tag works in some browsers, but not all (such as some mozilla-based ones). img 标签的 alt 属性适用于某些浏览器,但不是所有浏览器(例如一些基于 mozilla 的浏览器)。

The " right way " to do this is to use the title property.做到这一点的“ 正确方法”是使用 title 属性。

As Prestaul pointed out, the alt tag should work for images and title for links.正如 Prestaul 所指出的,alt 标签应该适用于图像和链接的标题。 However, this is also browser dependent...most browsers should implement functionality that displays this metadata as tooltips but they aren't required to do so.但是,这也取决于浏览器……大多数浏览器应该实现将元数据显示为工具提示的功能,但它们不是必需的。

正如 Joel Coehoom 指出的那样,我意识到我的图标实际上是一个背景图像,我创建了一个透明的.gif 图像,在背景顶部带有标题和 alt 属性,瞧——工具提示!

您可以在测试中使用标签 abbr 和标题属性,例如<abbr tittle="some text"> </abbr>作为答案https://stackoverflow.com/a/61601175/9442717

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

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