问题
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?
回答1:
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();
});
回答2:
Does that code you posted work? If not, it's probably because browsers disallow modification of iframe content for security reasons.
回答3:
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:
You have lost 1 level, you need modify innerHtml of body, but not document:
document.body.innerHTML = bla-bla
回答5:
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)
回答6:
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 :)
来源:https://stackoverflow.com/questions/2781003/how-to-insert-html-in-iframe