当访问者将鼠标悬停在图像上时,会出现替代文本。是否可以使用 javascript 对任何事件和元素模拟此行为?
在这一点上附加一个小跨度将是一个解决方案,但我想知道这是否可以在不向内容附加任何内容的情况下实现。
最佳答案
这是一种使用 <figcaption>
的解决方案发挥悬停工具提示的作用。
使用<figcaption>
因为工具提示将为您的标记赋予额外的语义含义。
figure.inline-image,
figure.inline-image img {
display: inline-block;
width: 260px;
height: 150px;
background-color: rgba(0, 0, 0, 1);
}
figcaption {
display: none;
position: relative;
top: -70px;
left: 40px;
padding: 3px;
width: auto;
border: 1px solid rgba(0, 0, 0, 1);
background-color: rgba(255, 255, 227, 1);
font-size: 12px;
}
figure.inline-image:hover figcaption {
display: inline-block;
}
<figure class="inline-image">
<img src="danish-flag.jpg" alt="Flag of Denmark" />
<figcaption>In the sky flies a red flag with a white cross whose vertical bar is shifted towards the flagpole.</figcaption>
</figure>
关于javascript - 模拟图像 alt 属性的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33598722/