我正在尝试使用DOMParser来解析HTML页面(在本例中为http://www.iana.org/domains/reserved)并打印出其<a>
元素及其目标。
我已在http://www.iana.org/处的控制台粘贴并运行以下代码:
var xhr = new XMLHttpRequest();
xhr.open("GET", "http://www.iana.org/domains/reserved");
xhr.onreadystatechange = function() {
if (xhr.readyState == XMLHttpRequest.DONE) {
var parser = new DOMParser();
var doc = parser.parseFromString(this.responseText, "text/html");
var links = doc.getElementsByTagName("a");
for (var i = 0; i < links.length; ++i) {
console.log(links[i].text + " goes to " + links[i].href);
}
}
}
xhr.send();
在Firefox上,我得到了预期的结果:
Domains goes to http://www.iana.org/domains
Numbers goes to http://www.iana.org/numbers
Protocols goes to http://www.iana.org/protocols
然而,在Chrome上,似乎元素href是空白的,我得到以下输出:
Domains goes to
Numbers goes to
Protocols goes to
为什么hrefs没有出现在Chrome中?