请看下面的代码:
<abbr title="World Health Organization">WHO</abbr>
我们可以给缩写标签(abbr tag)的title属性添加样式吗?这样我们就可以使用默认的tooltip而不是自定义的tooltip了吗?
实际上,Alex Mcp的答案是错误的。对于现代浏览器,可以完全使用CSS来完成这个任务。但是,可以在旧版浏览器中使用JavaScript作为备选方案。
abbr {
position: relative;
}
abbr:hover::after {
position: absolute;
bottom: 100%;
left: 100%;
display: block;
padding: 1em;
background: yellow;
content: attr(title);
}
当悬停在缩写标签(abbr)上时,这将使用标题中的属性内容在缩写标签的右上方添加一个绝对定位的伪元素。
content: attr(title);
没有起作用。没有文字显示。 - Pacerierposition: relative;
,它实际上是有效的。 - user1381553我正在寻找类似的东西,找到了以下替代方案(在Mac上测试过Firefox、Safari和Chrome,在IE 7-10上点击时有效),基于这个链接:
HTML:
<abbr title="My Custom Abbreviation" name="">Abbreviation</abbr>
jQuery:
$('[title]').each( function() {
var mytitle = $(this);
mytitle.data('title',mytitle.attr('title')); // get title attribute value
mytitle.attr('name', mytitle.attr('title')); // add title attribute value to NAME attribute
mytitle.removeAttr('title'); // remove the title attribute, removing browser tooltip
});
CSS:
abbr {
position: relative;
}
abbr:hover::after {
position: absolute;
bottom: 100%;
left: 100%;
display: block;
padding: 1em;
background: yellow;
content: attr(name);
}
width:max-content;
。这将提供一行工具提示。此外,我们可以使用 data-title
替代 name 属性。 - the Hutt您可以为缩写版本设置样式,例如在这种情况下是“WHO”。但无法控制弹出框的外观,因为它由浏览器/操作系统控制。
您可以通过对任何abbr
标签应用一些JQuery技术来解决此限制 - 这将显示一个div
,其中的内容与调用它的标签上的title
相同。