【问题标题】:How to load a webpage in a div? [closed]如何在 div 中加载网页? [关闭]
【发布时间】:2014-10-08 20:58:56
【问题描述】:

有点问题。

网页 whose doctype is not declared 包含 div id="abc" 。我需要在这个 doctype is <! DOCTYPE> 的 div 中加载一个网页。他们在quirk mode 中呈现的主要问题,这会导致外观问题。

div "abc" 是一个弹出窗口,具有绝对位置。

Content is loaded on user request , content is dynamic and built using PHP 
and can't provide url to achieve this .

有任何方法可以在不更改任何页面的内容类型的情况下获得与 HTML5 相同的外观。

【问题讨论】:

  • 试试 因为html文件可以通过Ajax动态加载
  • @TheOneandOnlyChemistryBlob Cotent 是根据用户请求加载的,所以我不能使用
  • 好的...您可以使用 Ajax 并根据用户的请求回显正确的内容吗?
  • @TheOneandOnlyChemistryBlob echo 不起作用,因为 doctype 不同。
  • 好的...知道了 ---> iframe

标签: javascript jquery css html


【解决方案1】:

因为 div 容器是您当前网站的一部分,并且它接受来自主网站的 css 样式和其他内容。当您在 div 容器中启动新的 html 标记时,您的 HTML 代码甚至无效。

使用 iframe 标签代替它专门用于创建“网站中的网站”:

<iframe src="http//path/to/my/site" height="1000" width="500"></iframe> 

有了这个,即使是 w3c 验证器也不会有问题,并且您的 css 和脚本不会被之前包含或执行的任何其他脚本覆盖。

编辑:

如果您需要按照您告诉我的那样动态加载内容,请使用 JQuery 更改 iframe 的 src:

如果iframe一开始没有初始化,直接隐藏即可:

<iframe id="content_iframe" src="" height="1000" width="500" style="display: none";></iframe>

然后稍后使用 jQuery,通过 ajax 或任何其他方式加载您的内容:

<script type="text/javascript">
    $(document).ready(function() {
        $.ajax({
          url: "path/to/my/url-to-get-my-url-or-content.php",
          type: "post",
          data: any: var },
          success: function(text) {
              //Lets assume I got the target url for the iframe, then just alter its src tag and make it visible:
              $("#content_iframe").attr("src", text);
              //And make it visible with a cool effect :P
              $("#content_iframe").show("slow");

              //Incase you do not have the URL but the raw content, just use function html instead and make it visible same way:
              $("#content_iframe").html(text);
              $("#content_iframe").show("slow");
          },
          error: function() {
               alert("Something went wrong, oops!");
          }
        });
    });
</script>

你完成了!

【讨论】:

  • 我必须动态添加内容。
  • 使用 JavaScript 还是我们在谈论 PHP?我猜是第一个,但只是在我在这里给出答案之前确定......
  • 但是您可以隐藏 iframe 并放置一个空的 src 标签,然后使用 JQuery 动态放置内容...
  • 页面使用 PHP 在服务器上构建,并使用 Ajax 调用发送回调用方函数。
  • 我得到了你的答案,但无法提供网址。
猜你喜欢
  • 1970-01-01
  • 2012-05-24
  • 1970-01-01
  • 1970-01-01
  • 2012-04-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-04
相关资源
最近更新 更多