【发布时间】:2019-03-30 16:00:44
【问题描述】:
我正在尝试获取多个 YouTube 缩略图中的 maxresdefault。一些 YouTube 视频根本没有高分辨率缩略图照片,所以我想用 img 元素上的 onError 道具来捕捉它。出于某种原因,当我收到 404 img 错误时,我的功能没有触发。有任何想法吗?提前致谢。
class FeaturedVideo extends Component<Props> {
addDefaultSrc = (e) => {
e.target.src = this.props.background.replace("maxresdefault", "hqdefault")
}
renderVideo = (props) => (
<div
style={{
width: "100%",
height: "100%",
backgroundSize: "contain",
}}
className="featured-community-video"
>
<img onError={this.addDefaultSrc} src={props.background} alt="" />
<div className="featured-community-video-title">
<h2 style={{ fontSize: "0.8em" }}>WATCH</h2>
<h1 style={{ fontSize: props.titleFontSize }}>{props.title}</h1>
</div>
</div>
)
render() {
return (
<div
key={this.props.postId}
style={{
width: this.props.width,
height: "50%",
}}
className="featured-community-video-container"
>
<Link to={routeCodes.POST_DETAILS(this.props.postId)}>
{this.renderVideo(this.props)}
</Link>
</div>
)
}
}
【问题讨论】:
标签: javascript reactjs image youtube onerror