[英]Use jQuery to Insert HTML into an iFrame Body Tag
I am using a hosted CMS that renders an iFrame within another iFrames. 我正在使用托管的CMS,在另一个iFrame中呈现iFrame。 These iFrames are loaded from the same domain but since this is a hosted CMS I do not have access to these iFrames directly. 这些iFrame是从同一个域加载的,但由于这是托管CMS,因此我无法直接访问这些iFrame。 Is it possible, using jQuery, to insert HTML content into the body
tag of the iFrame with the id of re_contentIframe
? 是否有可能使用jQuery将HTML内容插入到id为re_contentIframe
的iFrame的body
标签中?
Here is how the code renders on my site: 以下是代码在我的网站上呈现的方式:
<div class="editor">
<iframe id="editorf" frameborder="0" src="/ForumEditor.aspx?ForumID=2221&TopicID=-1&NoTemplate=False&Internal=False" style="display: inline;">
<html>
<head></head>
<body>
<!-- CODE -->
<iframe id="re_contentIframe" frameborder="0" src="javascript:'<html></html>';">
<html>
<head></head>
<body> <!-- THIS IS WHAT I WANT TO TARGET --> </body>
</html>
</iframe>
<!-- CODE -->
</body>
</html>
</iframe>
</div>
I tried using the following code but nothing happens with this code (including no errors): 我尝试使用以下代码但此代码没有任何反应(包括没有错误):
$(function() {
$( "#test" ).click(function() {
$("#re_contentIframe").contents().find("body").html("<p>new HTML content goes here</p>");
});
});
The problem is that you are trying to access a nested frame. 问题是您正在尝试访问嵌套框架。
The #re_contentIframe
frame is nested within #editorf
, which is subsequently nested within your document. #re_contentIframe
框架嵌套在#editorf
,随后嵌套在您的文档中。
Try: 尝试:
$('#re_contentIframe').contents().find('body').find('#editorf').contents()
Fiddle: http://jsfiddle.net/8VP4y/3/ 小提琴: http : //jsfiddle.net/8VP4y/3/
haven't checked it ,might help : 没检查过,可能有帮助:
var frameObject = document.getElementById('editorf');
var frameContent = frameObject.contentDocument ||
frameObject.contentWindow.document;
var insideIframe = frameContent.getElementById('re_contentIframe');
var insideIframeContent = insideIframeObject.contentDocument ||
insideIframeObject.contentWindow.document;
var $body = $('body',insideIframeContent);
$body.html('<div>contentupdated</div>');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.