HTML - Remove iframe Display

主宰稳场 提交于 2021-02-11 16:59:23

问题


I created an HTML page (main page) with an iframe so when I click the button the iframe will display covering the whole page. But I am having a problem on how to remove it whenever I click the back button from the iframe to the main page.

Here is my code:

function displayIframe() {
  document.getElementById("iframeDisplay").innerHTML = "<iframe src=\"./iframe/iframe.html\" height=\"100%\" width=\"100%\" ></iframe>";
}
<div id="iframeDisplay"> </div>
<button onclick="displayIframe()">Display Iframe</button>

Can you help me on how can I achieve closing the iframe via javascript or jquery


回答1:


You could wrap the iframe in a div and add the back button and the iframe in that div.

Add the click listener to the back button and remove the Iframe on button click.

Example:

function back() {
    document.getElementById("iframe").remove();
}
.back-button {
            position: absolute;
            right: 2px;
        }
<div style="width: 100%; height: 100%">
    <button class="back-button" onclick="back()">Back</button>
    <iframe id="iframe" src="http://google.com" height="100%" width="100%">
    
    </iframe>
</div>



回答2:


Since the source of your frame is on the same domain (even site) as the containing page, you should be able to reach from frame's code into the parent.

window.parent.document.querySelector('iframe').style.display = 'none';


来源:https://stackoverflow.com/questions/60841904/html-remove-iframe-display

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!