本文介绍了将内容插入iFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 限时删除!! 我试图将一些内容插入到'空白'iFrame中,但是没有插入任何内容。 HTML: < iframe id =iframe>< / iframe> JS: 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'); }); }); 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);});I am calling the ready function so I don't understand why it is not inserting. 解决方案 You really don't need jQuery for that:var doc = document.getElementById('iframe').contentWindow.document;doc.open();doc.write('Test');doc.close();jsFiddle DemoIf you absolutely have to use jQuery, you should use contents():var $iframe = $('#iframe');$iframe.ready(function() { $iframe.contents().find("body").append('Test');});jsFiddle DemoPlease 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 1403页,肝出来的..