How to dynamic load the document content into an i

2019-02-18 23:12发布

问题:

I would like to make something like this

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

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.

回答1:

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.

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

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

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

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

where myDiv is the ID of your div.



回答2:

$('#iframeId').contents().find('html').html(yourContent);