简体   繁体   English

如何在 div 中加载页面?

[英]How do I load a page in a div?

I've been told that using Divs instead of iframes is the way forward, so I'm using frames from my banner and main body.有人告诉我,使用 Divs 而不是 iframe 是前进的方向,所以我使用的是横幅和主体中的框架。 How do I load my index.html into my div?如何将我的 index.html 加载到我的 div 中?

did u try.load() of jquery.您是否尝试过 jquery 的 load()。 with server side technology you can do this easily.使用服务器端技术,您可以轻松做到这一点。

Using jQuery:使用 jQuery:

jQuery('#myDiv').load('http://example.com/somefile.html');

Without jQuery:不带 jQuery:

var request = new XMLHttpRequest();
request.open('GET', 'http://example.com/somefile.html', true);
request.onreadystatechange = function (anEvent) {
   if (request.readyState == 4) {
      if(request.status == 200) {
         document.getElementById("myDiv").innerHTML = request.responseText;
      }
   }
};
request.send(null);

Typically I use this approach to load small snippets of content dynamically.通常我使用这种方法来动态加载内容的小片段。 It's probably a bad idea to use a div if you're loading an extremely large amount of content (like an entire page).如果要加载大量内容(如整个页面),使用 div 可能是个坏主意。 An iframe would be better for that scenario. iframe 对于这种情况会更好。

$(".divClassHere").load("url path here");

However, it sounds to me more like you're after a MasterPage structure (if you've got ASP.NET) or file imports但是,在我看来,您更像是在使用MasterPage结构(如果您有 ASP.NET)或文件导入

Some people say that objects instead of iframes is the way forward.有人说对象而不是 iframe 是前进的方向。 But divs and iframes are completely different things.但是 div 和 iframe 是完全不同的东西。 An iframe is like a separate page within the page. iframe 就像页面中的单独页面。 It has a different context, different scripts, different css... A similar result can be achieved using object.它有不同的上下文、不同的脚本、不同的 css... 使用 object 可以实现类似的结果。 But nothing like this can be done with a div.但是这样的事情不能用 div 完成。

You can load content into a div.可以将内容加载到 div 中。 You can use javascript to alter the content of a div.您可以使用 javascript 来更改 div 的内容。 With an ajax call you can get the contents of an url which can be put in the div using javascript.通过 ajax 调用,您可以获得 url 的内容,可以使用 javascript 将其放入 div 中。

But this is entirely different from iframes.但这与 iframe 完全不同。

You create a separate page for each distinct document you have, then you use some form of template or include system to duplicate common content automatically.您为每个不同的文档创建一个单独的页面,然后使用某种形式的模板包含系统来自动复制常见内容。

Which serverside language you are using?您使用的是哪种服务器端语言?

first try to use your Serverside language to achive this.首先尝试使用您的服务器端语言来实现这一点。 For example if you are using PHP you can use include例如,如果您使用 PHP 您可以使用include

and if you are usind coldfusion consider using cfinclude如果您使用 coldfusion 考虑使用cfinclude

and if you a told to do it through javascript then the only way is Ajax.如果您被告知通过 javascript 进行操作,那么唯一的方法是 Ajax。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM