如何将文档内容动态加载到iframe中?

如何将文档内容动态加载到iframe中?

问题描述:

我想这样做

|--------------------------|
|                A         |
|        ____________      |
|       |            |     |
|       |            |     |
|       |      B     |     |
|       |            |     |
|       |____________|     |
|                          |
|__________________________|

B页面在A页面内,我要将所有js,css插入A页面,我想分离B页面,我设计它使用iFrame,并动态获取b页面。但是如果我使用src在B页面iframe,A页面js,css无法控制它...我如何完全插入所有的B页面代码在A页面,但不需要复制&粘贴所有从B页到A页的代码?谢谢。

The B page is inside the A page, and I want to insert all the js, css, into the A page, and I want to separate the B page, and I design it to use a iFrame, and get the b page dynamically. But if I use the src in the B page iframe, the A page js, css can't control it... How can I exactly insert all the B page code inside the A page, but don't need copy&paste all the code from B page to A page? Thank you.

您可能需要查看一个AJAX解决方案。这样就避免了iframe和相关的复杂性。在A页面上,您可以创建具有特定ID的div标签。然后使用AJAX可以将B页面的内容加载到div标签中。

You may want to look at an AJAX solution. This way you avoid the iframe and associated complexity. On the A page you can create a div tag with a specific id. Then using AJAX you can load the content of the B page into the div tag.

然后,可以直接通过A页面上的代码来操作加载的HTML,因为它本质上是一页。

The HTML that is loaded can then directly be manipulated by the code on your A page since it is essentially one page.

如果你使用纯javascript,那么你会发现你需要的所有信息在本教程

If you are using plain javascript then you will find all the info you need in this tutorial.

如果在另一方面,你希望使用jQuery,简单为:

If on the other hand you wish to use jQuery then it is as simple as:

  $('#myDiv').load('B.html');

其中myDiv是您的div的ID。

where myDiv is the ID of your div.