5

大家好:我需要在 iframe 中插入一个 html 字符串,如下所示:

……

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

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

有没有办法做到这一点?

4

6 回答 6

7
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();     
}); 
于 2011-04-22T04:05:15.977 回答
1

您发布的代码是否有效?如果不是,可能是因为浏览器出于安全原因不允许修改 iframe 内容。

于 2010-05-06T12:27:33.477 回答
0

或者,如果不确定父元素,您可以这样做:

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

至少这在我的情况下完成了工作:)

于 2014-10-22T10:03:13.507 回答
0

你已经失去了1级,你需要修改正文的innerHtml,而不是文档:

document.body.innerHTML = bla-bla
于 2010-05-06T12:32:09.330 回答
0

看起来您可以对身体进行重新审视,所以我不明白为什么不这样做:

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

于 2010-05-06T12:28:51.713 回答
0

remove()除非您使用 iframe 并使用“附加(html)”,否则您无法插入 iframe 。您可以将其插入 iframe 正文中,例如

$('body',parent.$("#indexIframe")[0].contentWindow.document).html(html)
于 2014-08-29T19:22:50.073 回答