简体   繁体   中英

How to append element to parent body from an iframe?

I have this code in the iframe.

<script type="text/javascript">
    $(document).ready(function(){
        $("body").append($("#ctap").html());
    });
</script>

I want to append #ctap 's html to parent body. How can I do that ?

尝试使用document.ready中的一段代码

parent.$("body").append($("#ctap").html());

If the parent body you should have something like this:

var iFrame = $('#iframe_id');
var iContent = iFrame.contents().find("#ctap").html();
$("body").append( iContent );

To be able to access the content of the host page from the iframe they must have exactly the same location (hostname and port) due to cross domain scripting restrictions.

See How to remove iframe from parent page using a function inside the iframe?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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