html - img::after 在 src 改变时消失

标签 html css

这是我的 React 代码的一部分

.jsx代码

<img draggable={false} data-score={-1 * score} src={AddressConfig.fileServer + "?id=" + photos} onError={this.onError} style={{display:'inline-block'}}/> 
......
onError = (e) => {
        e.target.src = 'data:image/jpeg;base64,/...........'//valid img url
    }

.css代码

img::after{
        content:attr(data-score);
        display: block;
        position: absolute;
        height: 15px;
        width:15px;
    }

当 onerror 事件没有触发时,一切正常,但是当它触发时,::after 元素将消失。我真的很困惑......

最佳答案

:after:before 不受 img 元素支持。

参见 https://developer.mozilla.org/en-US/docs/Web/CSS/%3A%3Aafter#Caveats

The pseudo-elements generated by ::before and ::after are contained by the element's formatting box, and thus ::before and ::after don't apply to replaced elements such as img, or to br elements.

关于html - img::after 在 src 改变时消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46499337/

相关文章:

html - CSS/HTML 屏幕尺寸自适应 block /图 block

html - 读取 aria-live 区域的标签

css - 缩小图像时,图像在边界处被裁剪。如何修复该错误?

JQuery:如何将 CSS 类添加到 HTML 按钮?

html - 文字不换行,无论如何

html - 扩展可滚动的 div 以匹配另一个 div 的底部

css - 相同的列大小

html - 让兄弟用完动态宽度元素的宽度

javascript - jQuery UI 可调整大小的重叠问题

html - 使用 CSS 方形偷工减料 - 这样可以吗?