如何显示 HTML <input> 的工具提示文本元素使用 CSS 样式和 data-title 属性? [英] How do I display tooltip text for HTML <input> element using CSS style and data-title attribute?

查看:19
本文介绍了如何显示 HTML <input> 的工具提示文本元素使用 CSS 样式和 data-title 属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 HTML 控件的 data-title 属性显示类似文本的工具提示.

I am attempting to display tooltip like text using the data-title attribute for an HTML control.

我对元素使用了以下技术,效果很好.

I used the following technique for a element, and it works fine.

HTML 元素:

<span class="spanNewID" data-title="Tooltip Text">816631-20319G14440 </span>

CSS 样式:

span.spanNewID[data-title]:hover:after {
    opacity: 1;
    transition: all 0.1s ease 0.5s;
    visibility: visible;
}

span.spanNewID[data-title]:after {
    content: attr(data-title);
    background-color: lightblue;
    color: #111;
    font-size: 12pt;
    font-weight: bold;
    position: absolute;
    padding: 1px 5px 2px 5px;
    bottom: -1.6em;
    left: 100%;
    white-space: nowrap;
    box-shadow: 1px 1px 3px #222222;
    opacity: 0;
    border: 1px solid #111111;
    z-index: 99999;
    visibility: hidden;
}

span.spanNewID[data-title] {
    position: relative;
}

上面的代码片段可以正确显示我的基于 css 的工具提示.

The above code snippet works to correctly display my css based tooltip.

我正在尝试将相同的选择器应用于元素.考虑以下几点:

I am trying to apply the same selector to an element. Consider the following:

HTML 元素:

<input type="submit" value="Click Me" class="inuse" data-title="Input ELement Help"> </input>

CSS 样式:

input.inuse[data-title]:hover:after {
    opacity: 1;
    transition: all 0.1s ease 0.5s;
    visibility: visible;
}

input.inuse[data-title]:after {
    content: attr(data-title);
    background-color: lightblue;
    color: #111;
    font-size: 12pt;
    font-weight: bold;
    position: absolute;
    padding: 1px 5px 2px 5px;
    bottom: -1.6em;
    left: 100%;
    white-space: nowrap;
    box-shadow: 1px 1px 3px #222222;
    opacity: 0;
    border: 1px solid #111111;
    z-index: 99999;
    visibility: hidden;
}

input.inuse[data-title] {
    position: relative;
}

在这种情况下不显示悬停文本/工具提示.我没有看到任何错误.页面上没有可见的更改.我尝试在 ChromeCSS 选择器测试器"中使用 css 选择器,并且选择器按预期工作.

The hover text/tooltip does not display in this case. I do not see any errors. There are no visible changes on the page. I attempted to use the css selector in the Chrome "CSS Selector Tester" and the selector works as expected.

我在这里遗漏了什么/做错了什么?

What am I missing/doing wrong here?

谢谢,JohnB

推荐答案

我自己在谷歌上搜索了一些,并得出了一些有趣的信息.首先,pseduo-selectors :before 和 :after 应该用于容器元素.

I did a bit of Googling myself, and came up with some interesting information. First off, pseduo-selectors :before and :after should be used on container elements.

可能你可以使用 而不是 并达到你想要的效果:

Potentially you could use <button></button> instead of <input> and achieve the effect you desire:

.inuse[data-title]:hover:after {
    opacity: 1;
    transition: all 0.1s ease 0.5s;
    visibility: visible;
}

.inuse[data-title]:after {
    content: attr(data-title);
    background-color: lightblue;
    color: #111;
    font-size: 12pt;
    font-weight: bold;
    position: absolute;
    padding: 1px 5px 2px 5px;
    bottom: -1.6em;
    left: 100%;
    white-space: nowrap;
    box-shadow: 1px 1px 3px #222222;
    opacity: 0;
    border: 1px solid #111111;
    z-index: 99999;
    visibility: hidden;
}

.inuse[data-title] {
    position: relative;
}

<button type="submit" value="Click Me" class="inuse" data-title="Input ELement Help">Click Me</button>

或者,我相信您已经考虑过这一点,但无论如何都值得一提,只需使用 title 属性:

Or, and I'm sure you've considered this but it's worth mentioning anyway, just use the title attribute:

<input type="submit" value="Click Me" class="inuse" data-title="Input ELement Help" title="Input ELement Help">

这篇关于如何显示 HTML &lt;input&gt; 的工具提示文本元素使用 CSS 样式和 data-title 属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆