【发布时间】:2011-12-10 11:26:16
【问题描述】:
我创建了一个 iframe 并将源设置为一个已经创建的空页面。问题是如何在空页面的 div 内动态添加一些 html 块(html 已经存储在变量中),然后将其显示在 iframe 上。我正在使用 jquery。谢谢
【问题讨论】:
我创建了一个 iframe 并将源设置为一个已经创建的空页面。问题是如何在空页面的 div 内动态添加一些 html 块(html 已经存储在变量中),然后将其显示在 iframe 上。我正在使用 jquery。谢谢
【问题讨论】:
选择 iframe,然后使用 .contents() 访问 iframe 的 jQuery 包装的文档对象(仅限同一域!)。
var chunk = "<a>Test</a>";
var iframeBody = $("#id-selector-of-iframe").contents().find("body");
iframeBody.append(chunk);
//Or, if you want to overwrite the body:
//iframeBody.html(chunk);
【讨论】: