Cannot modify content of iframe, what is wrong?

前端 未结 3 1190
迷失自我
迷失自我 2020-11-29 09:52

I am trying to modify content of an iframe but it does not work.

This is my main.html


Main page
&         


        
相关标签:
3条回答
  • 2020-11-29 10:04

    Please don't forget the cross-domain policy, otherwise it won't work.

    Live demo: http://jsfiddle.net/oscarj24/wTWjF/

    (Just to know, I am not using a 404 page, take a look)

    Try this:

    $(document).ready(function(){
        $('#ifr').ready(function(){
            $(this).contents().find('body').html('Hey, I have changed the body content yay!');
        });
    });​
    
    0 讨论(0)
  • 2020-11-29 10:08

    You would do something like this:

    You set the src attribute of your iFrame to a blank page (blank.htm) or "", then in the ready handler you change the src to ifr.htm.

    $(document).ready(
        $('#ifr').load(function(){
            $(this).contents().find('body').html('Hey, i`ve changed content of <body>!    Yay!!!');
        });
        $('#ifr').attr("src", "ifr.htm");
    });
    
    0 讨论(0)
  • 2020-11-29 10:14

    Assuming you are honoring the cross-domain policy, this sounds like a timing issue. Chances are your iframe has already finished loading when the document.ready of the parent page fires.

    You could try changing your frame.load to frame.ready as .ready will fire immediately if it is bound after the ready event has already triggered.

    0 讨论(0)
提交回复
热议问题