用通过 AJAX 检索的内容替换 HTML 页面 [英] Replace HTML page with contents retrieved via AJAX

查看:37
本文介绍了用通过 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();

这篇关于用通过 AJAX 检索的内容替换 HTML 页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆