Javascript 使用parseFragment在firefox插件中解析HTML
这里是parseFunctionJavascript 使用parseFragment在firefox插件中解析HTML,javascript,parsing,firefox,firefox-addon,Javascript,Parsing,Firefox,Firefox Addon,这里是parseFunction Ajax: { ParseHTML: function(aHTMLString) { var html = document.implementation.createDocument("http://www.w3.org/1999/xhtml", "html", null), body = document.createElementNS("http://www.w3.org/1999/xhtml", "body
Ajax:
{
ParseHTML: function(aHTMLString)
{
var html = document.implementation.createDocument("http://www.w3.org/1999/xhtml", "html", null),
body = document.createElementNS("http://www.w3.org/1999/xhtml", "body");
html.documentElement.appendChild(body);
body.appendChild(Components.classes["@mozilla.org/feed-unescapehtml;1"]
.getService(Components.interfaces.nsIScriptableUnescapeHTML)
.parseFragment(aHTMLString, false, null, body));
return body;
}
}
在这里,我尝试在http响应中使用解析(对代码进行sanatize):
但是,当我尝试使用:
newdoc.getElementById('teste');
它返回错误:TypeError:newdoc.getElementById不是函数
我做错什么了吗?它与documentType或其他东西有关
此外,此函数将删除
a
标记中的所有href=”“
属性。例如,可能问题与此相关。getElementById
仅在文档
对象上定义,因为文档中的ID必须是唯一的。因此,在元素对象上定义getElementById
是没有意义的
以下方法将起作用:
newdoc.querySelector('#teste');
newdoc.ownerDocument.getElementById('teste');
至于创建文档,您可能需要或想要使用(使用
HTML
)。很酷,它解决了getelementById问题。。。我刚刚将返回值替换为return body.ownerDocument@StiveKnx如果要返回文档,只需使用return html代码>。此变量相当于body.ownerDocument
。这解决了DOM的问题,但仍然从a
元素中删除所有href=atributes,有什么原因吗?@StiveKnx MDN的nsIScriptableUnescapeHTML
文档可能已过时(),在处标记为过时。如果您只想解析HTML中的字符串,我建议您看看。谢谢,我现在正在使用DOMParser,现在没有问题或bug,解决了这两个问题!我首先尝试了nsIScriptableUnescapeHTML,因为mozilla插件评论告诉我在请求中使用它,所以。谢谢
newdoc.querySelector('#teste');
newdoc.ownerDocument.getElementById('teste');