如何在 Firefox 中从 JavaScript 解析 HTML? [英] How to parse HTML from JavaScript in Firefox?
本文介绍了如何在 Firefox 中从 JavaScript 解析 HTML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在 Firefox 中解析(获取 DOM 树)XmlHttpRequest 的 HTML 结果的最佳方法是什么?
What is the best way to parse (get a DOM tree of) a HTML result of XmlHttpRequest in Firefox?
我没有DOM树,我想获得它.
I do not have the DOM tree, I want to acquire it.
XmlHttpRequest 的responseXML"只有在结果是实际的 XML 时才有效,所以我只有 responseText 可以使用.
XmlHttpRequest's "responseXML" works only when the result is actual XML, so I have only responseText to work with.
innerHTML hack 似乎不适用于完整的 HTML 文档(在 <html></html> 中). - 结果证明它工作正常.
The innerHTML hack doesn't seem to work with a complete HTML document (in <html></html>). - turns out it works fine.
推荐答案
innerHTML
应该可以正常工作,例如
innerHTML
should work just fine, e.g.
// This would be after the Ajax request:
var myHTML = XHR.responseText;
var tempDiv = document.createElement('div');
tempDiv.innerHTML = myHTML.replace(/<script(.|s)*?/script>/g, '');
// tempDiv now has a DOM structure:
tempDiv.childNodes;
tempDiv.getElementsByTagName('a'); // etc. etc.
这篇关于如何在 Firefox 中从 JavaScript 解析 HTML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文