如何针对无效的 .mp4 格式文件进行测试以隐藏相应的容器?
<div id="video">
<video controls width="320" height="240">
<source src="<?php echo $row["username"]);?>" type="video/mp4">
</video>
</div>
这是我的尝试:
<script>
document._video = document.getElementById("video");
document._video.addEventListener('error',function(){
video.style.display = "none";
});
</script>
最佳答案
为什么不工作?因为你给div添加事件监听器不是视频标签的来源
<div id="video">
<video controls="controls" width="320" height="240">
<source src="aaaa" type="video/mp4"/>
</video>
</div>
<script>
var v = document.querySelector('#video');
var sources = v.querySelectorAll('source');
if (sources.length !== 0) {
var lastSource = sources[sources.length-1];
lastSource.addEventListener('error', function() {
v.style.display="none";
});
}
</script>
关于javascript - 视频源格式无效问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49057826/