解决Dplayer兼容IE10问题
Dplayer是调用HTML5的video标签调用原生播放器,video可以兼容IE9以上浏览器,但是Dplayer使用了仅支持IE11以上才支持的dataset属性,所以在IE10会有兼容问题,解决方法是增加dataset的兼容处理。
(function datasetModule(global, definition) { // non-exporting module magic dance
'use strict';
var
amd = 'amd',
exports = 'exports'; // keeps the method names for CommonJS / AMD from being compiled to single character variable
if (typeof define === 'function' && define[amd]) {
define(function definer() {
return definition(global);
});
} else if (typeof module === 'function' && module[exports]) {
module[exports] = definition(global);
} else {
definition(global);
}
}(this, function datasetPolyfill(global) {
'use strict';
var
attribute,
attributes,
counter,
dash,
dataRegEx,
document = global.document,
hasEventListener,
length,
match,
mutationSupport,
test = document.createElement('_'),
DOMAttrModified = 'DOMAttrModified';
function clearDataset(event) {
delete event.target._datasetCache;
}
function toCamelCase(string) {
return string.replace(dash, function (m, letter) {
return letter.toUpperCase();
});
}
function getDataset() {
var
dataset = {};
attributes = this.attributes;
for (counter = 0, length = attributes.length; counter < length; counter += 1) {
attribute = attributes[counter];
match = attribute.name.match(dataRegEx);
if (match) {
dataset[toCamelCase(match[1])] = attribute.value;
}
}
return dataset;
}
function mutation() {
if (hasEventListener) {
test.removeEventListener(DOMAttrModified, mutation, false);
} else {
test.detachEvent('on' + DOMAttrModified, mutation);
}
mutationSupport = true;
}
if (test.dataset !== undefined) {
return;
}
dash = /\-([a-z])/ig;
dataRegEx = /^data\-(.+)/;
hasEventListener = !!document.addEventListener;
mutationSupport = false;
if (hasEventListener) {
test.addEventListener(DOMAttrModified, mutation, false);
} else {
test.attachEvent('on' + DOMAttrModified, mutation);
}
// trigger event (if supported)
test.setAttribute('foo', 'bar');
Object.defineProperty(global.Element.prototype, 'dataset', {
get: mutationSupport ?
function get() {
if (!this._datasetCache) {
this._datasetCache = getDataset.call(this);
}
return this._datasetCache;
} : getDataset
});
if (mutationSupport && hasEventListener) { // < IE9 supports neither
document.addEventListener(DOMAttrModified, clearDataset, false);
}
}));
当前页面是本站的「Google AMP」版。查看和发表评论请点击:完整版 »