从 Firefox 4 开始,'error' 事件是dispatched on the <source> element。
您应该在唯一/最后一个来源上添加一个错误处理程序:
HTML
<video id="vid" controls>
<source src="dynamicsearch.mp4" type="video/mp4"></source>
<source src="otherdynamicsearch.avi" type="video/avi"></source>
</video>
JS
var v = document.querySelector('video#vid');
var sources = v.querySelectorAll('source');
if (sources.length !== 0) {
var lastSource = sources[sources.length-1];
lastSource.addEventListener('error', function() {
alert('uh oh');
});
}
jQuery
>
$('video source').last().on('error', function() {
alert('uh oh');
});
AngularJS
您可以创建一个错误处理指令(或只使用ng-error):
<video id="vid" controls>
<source src="dynamicsearch.mp4" type="video/mp4"></source>
<source src="otherdynamicsearch.avi" type="video/avi" ng-error="handleError()"></source>
</video>
错误处理指令的 link 函数应该在哪里(从 ng-error 复制):
element.on('error', function(event) {
scope.$apply(function() {
fn(scope, {$event:event});
});
});