How to insert html in iframe

2019-05-04 15:27发布

Hallo all: I need to insert a html string in a iframe as shown below:

....

var html = "<html><head><title>Titolo</title></head><body><p>body</p></body></html>"

jQuery('#popolaIframe').click(function() {
  parent.$("#indexIframe")[0].documentElement.innerHTML = html;     
}); 

Is there a way to achieve this?

6条回答
唯我独甜
2楼-- · 2019-05-04 15:49

Does that code you posted work? If not, it's probably because browsers disallow modification of iframe content for security reasons.

查看更多
戒情不戒烟
3楼-- · 2019-05-04 15:54

Looks like you can get a refrence to the body so I don't see why not:

http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_iframe_contentdocument

查看更多
干净又极端
4楼-- · 2019-05-04 15:56
var html = "<html><head><title>Titolo</title></head><body><p>body</p></body></html>"

jQuery('#popolaIframe').click(function() {
  var doc = parent.$("#indexIframe")[0].documentElement;
  doc.open();
  doc.write(html);
  doc.close();     
}); 
查看更多
【Aperson】
5楼-- · 2019-05-04 15:58

Alternitevely if not sure about the parent element you could do that:

var doc = $.find("#myIframe")[0].contentWindow.document; //that will look in the whole dom though
doc.open();
doc.write(html);

At least this did the job in my case :)

查看更多
神经病院院长
6楼-- · 2019-05-04 16:02

You have lost 1 level, you need modify innerHtml of body, but not document:

document.body.innerHTML = bla-bla
查看更多
劫难
7楼-- · 2019-05-04 16:09

You cannot insert into an iframe unless you remove() the iframe and use 'append(html)'. You can insert it inside iframes body like

$('body',parent.$("#indexIframe")[0].contentWindow.document).html(html)
查看更多
登录 后发表回答