gpt4 book ai didi

javascript - 将内容插入 iFrame

转载 作者:IT王子 更新时间:2023-10-29 03:09:05 24 4
gpt4 key购买 nike

我正在尝试将一些内容插入到“空白”iFrame 中,但没有插入任何内容。

HTML:

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

JS:

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

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

最佳答案

你真的不需要 jQuery:

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

jsFiddle Demo

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

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

jsFiddle Demo

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

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

关于javascript - 将内容插入 iFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21795761/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com