对 DOM importNode 的 IE 支持 [英] IE support for DOM importNode

查看:21
本文介绍了对 DOM importNode 的 IE 支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在网上搜索,我很确定我已经知道答案(否"),但我想检查一下:

IE 支持 importNode() 吗?有没有比遍历 DOM 和创建节点更好的选择?(我看过Anthony Holdener 的经典文章,但现在已经一年多了,我希望 IE 已经发展,或者有人有其他解决方法)

谢谢.

解决方案

我还没有听说这已经改变了,并且在 John Resig 最近的帖子,他说:

<块引用>

Internet Explorer 不支持 importNode 或 AdoptNode

另请参阅有关 跨浏览器 importnode() 的 A List Apart 文章,因为它包括针对 Internet Explorer 的特定解决方法.

为后人引述,

<块引用>

我所有问题的解决方案毕竟是不使用 DOM 方法,而是使用我自己的实现.在这里,在它的所有荣耀中,是我以跨浏览器兼容方式编码的 importNode() 问题的最终解决方案:(换行标记为 » —Ed.)

if (!document.ELEMENT_NODE) {document.ELEMENT_NODE = 1;文档.ATTRIBUTE_NODE = 2;文档.TEXT_NODE = 3;document.CDATA_SECTION_NODE = 4;document.ENTITY_REFERENCE_NODE = 5;document.ENTITY_NODE = 6;document.PROCESSING_INSTRUCTION_NODE = 7;文档.COMMENT_NODE = 8;文档.DOCUMENT_NODE = 9;文档.DOCUMENT_TYPE_NODE = 10;document.DOCUMENT_FRAGMENT_NODE = 11;文档.NOTATION_NODE = 12;}document._importNode = 函数(节点,allChildren){开关(节点.节点类型){案例文档.ELEMENT_NODE:var newNode = document.createElement(node ».nodeName);/* 该节点是否有任何要添加的属性?*/if (node.attributes && node.attributes ».length >0)for (var i = 0; il = node.attributes.length;i 

<块引用>

这里正在使用:

var newNode = null,importNode = null;newNode = xhrResponse.responseXML.getElementsByTagName('title')[0].childNodes[0];if (newNode.nodeType != document.ELEMENT_NODE)newNode = newNode.nextSibling;如果(新节点){importNode = document._importNode(newNode, true);document.getElementById('divTitleContainer').appendChild(importedNode);如果(!document.importNode)document.getElementById('divTitleContainer').innerHTML = document.getElementById('divTitleContainer').innerHTML;}

I've been looking arround the web, and I'm fairly sure I already know the answer ("no"), but I'd like to check:

Does IE support importNode() yet? Is there a better alternative than walking the DOM and creating nodes? (I've seen the clasic article by Anthony Holdener but its more than a year old now, and I'm hoping that either IE has evolved, or someone has another workarround)

Thanks.

解决方案

I haven't heard this has changed yet, and in a recent post by John Resig, he states:

Internet Explorer doesn't support importNode or adoptNode

Also see this A List Apart article on cross-browser importnode() as it includes a specific work-around for Internet Explorer.

To quote for posterity,

The solution to all of my problems was to not use a DOM method after all, and instead use my own implementation. Here, in all of its glory, is my final solution to the importNode() problem coded in a cross-browser compliant way: (Line wraps marked » —Ed.)

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;
}

document._importNode = function(node, allChildren) {
  switch (node.nodeType) {
    case document.ELEMENT_NODE:
      var newNode = document.createElement(node »
.nodeName);
      /* does the node have any attributes to add? */
      if (node.attributes && node.attributes »
.length > 0)
        for (var i = 0; il = node.attributes.length;i < il)
          newNode.setAttribute(node.attributes[i].nodeName, 
          node.getAttribute(node.attributes[i++].nodeName));
      /* are we going after children too, and does the node have any? */
      if (allChildren && node.childNodes && node.childNodes.length > 0)
        for (var i = 0; il = node.childNodes.length; i < il)
          newNode.appendChild(document._importNode(node.childNodes[i++], allChildren));
      return newNode;
      break;
    case document.TEXT_NODE:
    case document.CDATA_SECTION_NODE:
    case document.COMMENT_NODE:
      return document.createTextNode(node.nodeValue);
      break;
  }
};

Here it is in use:

var newNode = null, importedNode = null;

newNode = xhrResponse.responseXML.getElementsByTagName('title')[0].childNodes[0];
if (newNode.nodeType != document.ELEMENT_NODE)
  newNode = newNode.nextSibling;
if (newNode) {
  importedNode = document._importNode(newNode, true);
  document.getElementById('divTitleContainer').appendChild(importedNode);
  if (!document.importNode)
    document.getElementById('divTitleContainer').innerHTML = document.getElementById('divTitleContainer').innerHTML;
}

这篇关于对 DOM importNode 的 IE 支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆