gpt4 book ai didi

javascript - 在 javascript 中,如何用 XML 替换窗口的全部内容?

转载 作者:行者123 更新时间:2023-11-28 07:30:54 25 4
gpt4 key购买 nike

我一直在尝试使用以下技术将一些 xml 内容传递到新的浏览器窗口:

    var newWindow = window.open('', '_blank');
newWindow.document.clear();
newWindow.document.open();
newWindow.document.write(xmlString);
newWindow.document.close();

但是,xml 被放置在 <body></body> 内新窗口中的标签。

如何使我的 xml 文档代表窗口的完整内容(无 <html></html> 标签)?

最佳答案

在写入文档之前,您需要删除一些元素。我会执行以下操作:

//The following performed on the new document you are creating

var tagsToDestroy = newWindow.document.querySelector('html');
newWindow.document.removeChild(tagsToDestroy);

//Then write your content and do whatever else

这将清除 html 标签,它们是 body 标签的父节点,以及任何子节点/孙节点/等等...

顺便说一句,我会避免使用关键字“new”来命名变量。

关于javascript - 在 javascript 中,如何用 XML 替换窗口的全部内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29133075/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com