简体   繁体   English

如何将网页加载到<div>元素中?

[英]How can I load a webpage into a <div> element?

I'm quite new to Javascript and I have a question about loading a webpage into a div element ('content-window') on my website. 我是Javascript的新手,我有一个关于在我的网站上将网页加载到div元素('content-window')的问题。 I want to open a webpage in that div, in this case http://www.fdsa.com?ID=1 . 我想在该div中打开一个网页,在这种情况下http://www.fdsa.com?ID=1 I've to write the following line in order to select the div element. 我要编写以下行来选择div元素。

var sidediv = document.getElementById('content-window');

Can someone tell me how can I load a webpage into this side div? 有人能告诉我如何将网页加载到这个边div? I've tried a lot (but I've failed of course). 我已经尝试了很多(但我当然失败了)。 For example, the following line won't work: 例如,以下行不起作用:

sidediv.location.assign = "http://www.fdsa.com?ID=1";

Thanks in advance! 提前致谢!

you should use an <iframe> tag as follows: 你应该使用<iframe>标签,如下所示:

<div>
  <iframe src="http://www.fdsa.com?ID=1">
</div>

or build it programatically: 或者以编程方式构建它:

document.getElementById("content-window").innerHTML='<iframe src="http://www.fdsa.com?ID=1">'

you could use more <iframe> information here: https://developer.mozilla.org/en/docs/Web/HTML/Element/iframe 您可以在此处使用更多<iframe>信息: https//developer.mozilla.org/en/docs/Web/HTML/Element/iframe

enjoy! 请享用!

HTMLCODE : HTMLCODE:

  <body onload="loadHtml()">
    <div id="content-window"></div>
  </body>

CSS CODE : CSS代码:

#content-window {
    width: 100%;

}

#content-window object{
    width: 100%;

}

JAVASCRIPT : JAVASCRIPT:

function loadHtml() {
    document.getElementById("content-window").innerHTML='<object type="text/html" data="http://www.fdsa.com?ID=1"></object>';
}

Another approach is AJAX, if you don't want it embeded inside and rather actually inline as part of the document. 另一种方法是AJAX,如果你不希望它embeded到内部而是实际inline作为文档的一部分。

The easiest (and for the sake of convenience, I will post it here) method utilizes jQuery, it's so easy it's incredible, obviously there are other methods but I have to post this one (I use it everywhere): 最简单的(为了方便起见,我会在这里发布)方法使用jQuery,它很容易它令人难以置信,显然还有其他方法,但我必须发布这个(我到处使用它):

Here is the function: 这是功能:

.load("http://url.com/")

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

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