转自:http://www.w3help.org/zh-cn/causes/BT9032
标准参考
无。
问题描述
只有 IE 的 HTMLDOMNode 有 replaceNode 与 swapNode 方法,其他浏览器则不支持。
造成的影响
该问题将造成某些使用本特性设计的功能在非 IE 浏览器中不能实现。
受影响的浏览器
IE6 IE7 IE8 |
---|
问题分析
HTMLDOMNode.replaceNode 与 HTMLDOMNode.swapNode 是 IE 浏览器的特性,简介如下:
-
replaceNode
-
oReplace = object.replaceNode(oNewNode)
将目标节点(object)替换为另一个节点(oNewNode),返回目标节点,即被替换的节点。
-
swapNode
-
oSwapped = object.swapNode(oNode)
交换 DOM 树中两个节点的位置,返回调用本方法的节点(object)。
关于 replaceNode 的更多信息,请参考 MSDN replaceNode Method。
关于 swapNode 的更多信息,请参考 MSDN swapNode Method。
解决方案
不要使用 IE 的特性设计页面代码。
或者针对非 IE 浏览器使用标准方法实现相同功能,如:
if(window.Node){ Node.prototype.replaceNode=function($target){ return this.parentNode.replaceChild($target,this); } Node.prototype.swapNode=function($target){ var $targetParent=$target.parentNode; var $targetNextSibling=$target.nextSibling; var $thisNode=this.parentNode.replaceChild($target,this); $targetNextSibling?$targetParent.insertBefore($thisNode,$targetNextSibling):$targetParent.appendChild($thisNode); return this; } }
注:IE 中没有内置函数 Node,即没有节点的 "构造函数"。
参见
知识库
相关问题
测试环境
操作系统版本: | Windows 7 Ultimate build 7600 |
---|---|
浏览器版本: | IE6 IE7 IE8 Firefox 3.6 Chrome 4.0.302.3 dev Safari 4.0.4 Opera 10.51 |
测试页面: | replaceNode_swapNode.html |
本文更新时间: | 2010-07-02 |
关键字
replaceNode swapNode
下面两个例子用来实现select选项中顺序的调整,因为用到了 swapNode,所以只支持IE浏览器:
http://www.webdm.cn/webcode/2e59cbd1-a01a-4e4d-908a-5aaec8f490ea.html
http://bbs.blueidea.com/thread-2802697-1-1.html
可以昨用上面的解决方法,做到各浏览器之间的兼容。