用通过 AJAX 检索的内容替换 HTML 页面
问题描述:
我有一个具有典型结构的 HTML 页面:
I have an HTML page with a typical structure:
<html>
<head>
<script src="..." ></script>
<style>...</style>
</head>
<body>
content
</body>
<script>
var success_callback = function(data) {
// REPLACE PAGE CONTENT & STRUCTURE WITH "data"
}
ajax(url, params, success_callback);
</script>
</html>
你觉得有可能吗?我已经尝试给 html 标记一个 id 并执行 $(id).replace(data);
没有成功.
Do you think it is possible ? I've already tried to give the html tag an id and doing $(id).replace(data);
with no success.
不要问我为什么,但这正是我需要的(我正在使用一个特殊的mashup builder"网站...这是一个很长的故事).
Don't ask me why, but that is what I need (I'm working with a special "mashup builder" site... it is a long story).
编辑:我忘了说必须执行接收内容中的脚本,甚至使用<script src="...">
.
EDIT : I forgot to say that scripts in the received content have to be executed, even external scripts included using <script src="...">
.
答
最简单的方法是使用以下方法设置新的 HTML 内容:
The simplest way is to set the new HTML content using:
document.open();
document.write(newContent);
document.close();