JavaScript:替换 XMLSerializer.serializeToString()?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4916327/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaScript: Replacement for XMLSerializer.serializeToString()?
提问by NRaf
I'm developing a website using the Seam framework and the RichFaces AJAX library (these isn't really all that important to the problem at hand - just some background).
我正在使用 Seam 框架和 RichFaces AJAX 库开发一个网站(这些对于手头的问题并不是那么重要——只是一些背景知识)。
I seem to have uncovered a bug, however, in RichFaces which, in certain instances, will cause AJAX-based updating to fail in IE8 (see here for more info: http://community.jboss.org/message/585737).
然而,我似乎在 RichFaces 中发现了一个错误,在某些情况下,它会导致 IE8 中基于 AJAX 的更新失败(有关更多信息,请参见此处:http: //community.jboss.org/message/585737)。
The following is the code where the exception is occurring:
以下是发生异常的代码:
var anchor = oldnode.parentNode;
if(!window.opera
&& !A4J.AJAX.isWebkitBreakingAmps()
&& oldnode.outerHTML
&& !oldnode.tagName.match( /(tbody|thead|tfoot|tr|th|td)/i ) ) {
LOG.debug("Replace content of node by outerHTML()");
if (!Sarissa._SARISSA_IS_IE || oldnode.tagName.toLowerCase()!="table") {
try {
oldnode.innerHTML = "";
} catch(e){
LOG.error("Error to clear node content by innerHTML "+e.message);
Sarissa.clearChildNodes(oldnode);
}
}
oldnode.outerHTML = new XMLSerializer().serializeToString(newnode);
}
The last line (the one with XMLSerializer) is where the exception is occurring in IE. I was wondering if anyone knows of any replacement method / library / etc I could use there (only on IE is fine). Thanks.
最后一行(带有 XMLSerializer 的那一行)是 IE 中发生异常的地方。我想知道是否有人知道我可以在那里使用的任何替换方法/库/等(仅在 IE 上很好)。谢谢。
EDIT: After doing some further research, it seems that the exception isn't being caused by XMLSerializer not being defined, rather it seems to happen when I try to assign the output from XMLSerializer to the outerHTML property of the oldnode.
编辑:经过进一步研究后,似乎异常不是由未定义 XMLSerializer 引起的,而是当我尝试将 XMLSerializer 的输出分配给 oldnode 的 outerHTML 属性时发生。
This is strange because it works most times but fails in only a couple of scenarios (this piece of the framework seems to be rather important).
这很奇怪,因为它在大多数情况下都有效,但仅在几个场景中失败(这部分框架似乎相当重要)。
Can anyone think of any reason as to when the output from XMLSerializer (which, from the what the debugger shows, look to be quite valid HTML) is nonassignable to the outerHTML property of an element?
任何人都可以想到什么时候 XMLSerializer 的输出(从调试器显示的内容来看,它看起来是非常有效的 HTML)不可分配给元素的 outerHTML 属性的任何原因?
The strangest thing is, if I were to clone the element (using cloneNode(true)
) and then set the outerHTML, it seems to work.
最奇怪的是,如果我要克隆元素(使用cloneNode(true)
)然后设置外层HTML,它似乎可以工作。
采纳答案by NRaf
I've since discovered the reason (a while ago actually). It turns out that IE semi-validates (it'll balk at some errors but ignore others) inserted HTML. It was throwing some 'unknown error' or something similar which was pretty much completely useless since it gave no indication as to what went wrong - just that something went wrong.
我已经发现了原因(实际上是不久前)。事实证明,IE 半验证(它会回避一些错误但忽略其他错误)插入的 HTML。它抛出了一些“未知错误”或类似的东西,这几乎是完全无用的,因为它没有说明出了什么问题——只是出了点问题。
In my case, it was because an <li /> was being inserted with a parent . If you're having similar issues, you may want to make sure you're not trying to be too clever with your HTML.
就我而言,这是因为 <li /> 与 parent 一起插入。如果您遇到类似的问题,您可能需要确保您的 HTML 不会太聪明。
回答by Tim Down
In IE you can simply use the xml
property of the XML node, provided newnode
really is an XML node rather than an HTML node:
在 IE 中,您可以简单地使用xml
XML 节点的属性,前提newnode
是确实是一个 XML 节点而不是 HTML 节点:
function serializeXmlNode(xmlNode) {
if (typeof window.XMLSerializer != "undefined") {
return (new window.XMLSerializer()).serializeToString(xmlNode);
} else if (typeof xmlNode.xml != "undefined") {
return xmlNode.xml;
}
return "";
}
oldnode.outerHTML = serializeXmlNode(newnode);
Update following question update
更新以下问题更新
I wouldn't use outerHTML
to replace an element. It's not universally supported. Instead, you could use a mix of innerHTML
and standard DOM methods as follows:
我不会outerHTML
用来替换元素。它没有得到普遍支持。相反,您可以混合使用innerHTML
标准 DOM 方法,如下所示:
var tempEl = document.createElement("div");
tempEl.innerHTML = serializeXmlNode(newnode);
oldnode.parentNode.replaceChild(oldnode, tempEl.firstChild);
回答by Stone
Edge case answer (mostly so I can find it later):
边缘情况的答案(主要是为了我以后可以找到它):
Sending HTML document as String to api to generate PDFs.
将 HTML 文档作为字符串发送到 api 以生成 PDF。
For anyone that needs to convert the document.body to a String and them submit it via a POST to a service to convert the document to a PDF - IE8 doesn't support XMLSerializer
. That being said you can use: $(document.body).html();
for IE8.
对于需要将 document.body 转换为 String 并通过 POST 将其提交到服务以将文档转换为 PDF 的任何人 - IE8 不支持XMLSerializer
. 话虽如此,您可以使用:$(document.body).html();
对于 IE8。
/**
* Decides the method by which to turn the document.body into a string that we can post to the PDF Api.
* Most browsers support XMLSerializer, for others (ie8) use jquery's html method to generate a string.
* @param xmldom - document.body
* @return - string representation of the document body
*/
function serializeXml(xmldom){
if (typeof XMLSerializer != "undefined"){
return (new XMLSerializer()).serializeToString(xmldom);
} else {
return $(xmldom).html();
}
}
Call it with: var dom = serializeXml(document.body);
调用它: var dom = serializeXml(document.body);
回答by Diego Mariano
The suggested solution does not work for me. Here is my solution to this problem.
建议的解决方案对我不起作用。这是我对这个问题的解决方案。
I replaced the line:
我替换了这一行:
oldnode.outerHTML = new XMLSerializer().serializeToString(newnode);
by this:
这样:
if(navigator.appName.indexOf('Internet Explorer')>0){
oldnode.outerHTML = newnode.xml
}else{
oldnode.outerHTML = new XMLSerializer().serializeToString(newnode);
}