David Flanagan 关于 JavaScript 的优秀书籍有一个示例,展示了如何在 IE 中执行 XPath 查询。在第五版的第 518 页,您可以看到示例 21-10 中的以下代码片段:
// In IE, the context must be an Element not a document,
// so if the context is a document, use the documentElement instead
if (context == doc) context = doc.documentElement;
return context.selectNodes(this.xpathText);
我发现这段代码是绝对必要的。看来(尽管我不明白为什么)在 IE XMLHttpRequest 上似乎随机返回对与接收到的 XML 对应的文档或 documentElement 的引用。这里发生了什么事?