如何使用jQuery替换整个HTML节点 [英] How do I replace the entire HTML node using jQuery
问题描述
我有一个字符串,如下所示:
I have a string which looks like:
<html><head><title>example</title></head><body>some example text</body></html>
我将此字符串作为结果返回给AJAX请求。
I get this string returned as a result to an AJAX request.
我希望浏览器呈现并显示该字符串。我的想法是做类似的事情:
I would like the browser to render and display that string. The idea would be to do something like:
$('html').parent().html(myString);
嗯,这不起作用。我试图使用IFRAME,但我还没弄明白如何让它工作。
Well, that doesn't work. I've attempted to use an IFRAME but I haven't figured out how to get that to work either.
注意:这是不可能的让我改变这个字符串。我也不可能在随后的服务器调用中重新生成此字符串(否则我只能将浏览器重定向到该URL)。
Note: It is impossible for me to change this string. It is also impossible for me to regenerate this string in a subsequent call to the server (otherwise I could just redirect the browser to that url).
推荐答案
document.open/write/close方法可以做你想要的:
The document.open/write/close methods will do what you want:
var newDoc = document.open("text/html", "replace");
newDoc.write(myString);
newDoc.close();
除非传入replace参数,否则document.open调用会添加页面历史记录。因此,用户必须再次点击两次才能转到上一页。
Unless you pass in the replace parameter, the document.open call adds page history. So users would have to click back twice to go to the previous page.
这篇关于如何使用jQuery替换整个HTML节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!