我在使用importnode的IE时遇到问题。我没有找到没有polyfill的最佳方法。有什么办法吗?
clones = el.childNodes;
for (var j = 0; j < clones.length; j++) {
if (clones[j].nodeType != 1) continue;
slides.appendChild(clones[j].cloneNode(true));
}
for (var j = 0; j < utilities.visibleSlides(el); j++) {
var clone = slides.childNodes[j],
cloneParent = document.createElement('div');
cloneParent.classList.add('tnt-slider-item');
cloneParent.classList.add('clone');
cloneParent.setAttribute('data-item', (slides.childNodes.length + j + 1));
cloneParent.appendChild(document.importNode(clone, true));
innerStage.appendChild(cloneParent);
}
答案 0 :(得分:0)
请检查您的IE浏览器版本,Document.importNode()方法支持IE 9+。 Internet Explorer 8和更早版本不支持importNode()方法。
如果要在IE8和更早版本中使用importNode()方法,则可以尝试使用以下polyfills:
'use strict';
if (!window.DocumentFragment && window.HTMLDocument) {
window.DocumentFragment = HTMLDocument;
}
if (!document.ELEMENT_NODE) {
document.ELEMENT_NODE = 1;
document.ATTRIBUTE_NODE = 2;
document.TEXT_NODE = 3;
document.CDATA_SECTION_NODE = 4;
document.ENTITY_REFERENCE_NODE = 5;
document.ENTITY_NODE = 6;
document.PROCESSING_INSTRUCTION_NODE = 7;
document.COMMENT_NODE = 8;
document.DOCUMENT_NODE = 9;
document.DOCUMENT_TYPE_NODE = 10;
document.DOCUMENT_FRAGMENT_NODE = 11;
document.NOTATION_NODE = 12;
}
if (!document.createElementNS) {
document.createElementNS = function (namespaceURI, qualifiedName) {
return document.createElement(qualifiedName);
};
}
if (!document.importNode) {
document.importNode = function (node, deep) {
var a, i, il;
switch (node.nodeType) {
case document.ELEMENT_NODE:
var newNode = document.createElementNS(node.namespaceURI, node.nodeName);
if (node.attributes && node.attributes.length > 0) {
for (i = 0, il = node.attributes.length; i < il; i++) {
a = node.attributes[i];
try {
newNode.setAttributeNS(a.namespaceURI, a.nodeName, node.getAttribute(a.nodeName));
}
catch (err) {
// ignore this error... doesn't seem to make a difference
}
}
}
if (deep && node.childNodes && node.childNodes.length > 0) {
for (i = 0, il = node.childNodes.length; i < il; i++) {
newNode.appendChild(document.importNode(node.childNodes[i], deep));
}
}
return newNode;
case document.TEXT_NODE:
case document.CDATA_SECTION_NODE:
return document.createTextNode(node.nodeValue);
case document.COMMENT_NODE:
return document.createComment(node.nodeValue);
case document.DOCUMENT_FRAGMENT_NODE:
docFragment = document.createDocumentFragment();
for (i = 0, il = node.childNodes.length; i < il; ++i) {
docFragment.appendChild(document.importNode(node.childNodes[i], deep));
}
return docFragment;
}
};
}