将内容插入 iFrame [英] Insert content into iFrame

查看:25
本文介绍了将内容插入 iFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一些内容插入到空白"iFrame 中,但是没有插入任何内容.

I am trying to insert some content into a 'blank' iFrame, however nothing is being inserted.

HTML:

<iframe id="iframe"></iframe>

JS:

$("#iframe").ready(function() {
    var $doc = $("#iframe").contentWindow.document;
    var $body = $("<body>").text("Test");
    $body.insertAfter($doc);
});

我正在调用 ready 函数,所以我不明白为什么它没有插入.

I am calling the ready function so I don't understand why it is not inserting.

推荐答案

你真的不需要 jQuery:

You really don't need jQuery for that:

var doc = document.getElementById('iframe').contentWindow.document;
doc.open();
doc.write('Test');
doc.close();

jsFiddle 演示

如果你绝对必须使用jQuery,你应该使用contents()::>

If you absolutely have to use jQuery, you should use contents():

var $iframe = $('#iframe');
$iframe.ready(function() {
    $iframe.contents().find("body").append('Test');
});

jsFiddle 演示

请不要忘记,如果您使用的是 jQuery,则需要按如下方式挂钩 DOMReady 函数:

Please don't forget that if you're using jQuery, you'll need to hook into the DOMReady function as follows:

$(function() {  
    var $iframe = $('#iframe');
    $iframe.ready(function() {
        $iframe.contents().find("body").append('Test');
    });
});

这篇关于将内容插入 iFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆