Replace Entire Content of Iframe

前端 未结 4 1159
鱼传尺愫
鱼传尺愫 2020-12-29 08:11

Is there a way to replace the ENTIRE contents of an iframe using Javascript (or Jquery)?

For example if my iframe had the following content...

blah o         


        
4条回答
  •  太阳男子
    2020-12-29 08:21

    I solved with this code:

    $('#IframeID').contents().find("html").html(html);
    

    Although this way you can work better in many of the cases, there will be more order:

    $('#IframeID').contents().find("head").html(head);
    $('#IframeID').contents().find("body").html(body);
    

    So, you can play with head and body separately, and a bad change on body will not affect your head.

    I tested on Chrome.

提交回复
热议问题