在浏览器JavaScript中创建Document的办法


1. 使用XMLHttpRequest

这种方法主要涉及XMLHttpRequest的两个属性responseType与response。在调用XMLHttpRequest实例的open方法之后并且在调用send方法之前,将responseType的属性值设置为字符串"document",然后当响应成功后XMLHttpRequest实例的response属性值便是一个Document实例,但是如果响应类型不匹配的话属性值则是null 

示例

 1 let xml = new XMLHttpRequest();
 2 
 3 xml.onload = () => {
 4 
 5 if (xml.status >= 200 && xml.status <= 299) {
 6 
 7 console.log(xml.response);
 8 
 9 }
10 
11 };
12 
13 xml.open("GET", "/");
14 
15 xml.responseType = "document";
16 
17 xml.send();

2.使用DOMParser对象 

示例

let doc = (new DOMParser).parseFromString("需要将其解析为Document的字符串", "text/html");

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM