如何从JavaScript在Firefox解析HTML? [英] How to parse HTML from JavaScript in Firefox?

查看:220
本文介绍了如何从JavaScript在Firefox解析HTML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是解析(得到的DOM树)的火狐XmlHtt prequest?

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.

XmlHtt prequest的responseXML的仅当结果是实际的XML,所以我只有responseText的一起工作。

XmlHttpRequest's "responseXML" works only when the result is actual XML, so I have only responseText to work with.

的innerHTML黑客似乎并没有一个完整的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.

这篇关于如何从JavaScript在Firefox解析HTML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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