使用jQuery将HTML插入到iFrame正文标签中 [英] Use jQuery to Insert HTML into an iFrame Body Tag
问题描述
我使用的托管CMS在另一个iFrame内呈现iFrame。这些iFrames从同一个域加载,但由于这是一个托管CMS,因此我无法直接访问这些iFrame。是否有可能使用jQuery将HTML内容插入到ID为 re_contentIframe
的iFrame的主体
标记中?
以下是代码在我的网站上呈现的方式:
<< ; div class =editor>
< iframe id =editorfframeborder =0src =/ ForumEditor.aspx?ForumID = 2221& TopicID = -1& NoTemplate = False& Internal = Falsestyle =display:inline; >
< html>
< head>< / head>
< body>
<! - 代码 - >
< iframe id =re_contentIframeframeborder =0src =javascript:< html>< / html>';>
< html>
< head>< / head>
< body> <! - 这就是我想要的目标 - > < /体>
< / html>
< / iframe>
<! - 代码 - >
< / body>
< / html>
< / iframe>
< / div>
我试过使用下面的代码,但是这段代码没有任何反应(包括没有错误):
$(function(){
$(#test).click(function(){
$ (#re_contentIframe)。contents()。find(body)。html(< p>新HTML内容转到< / p>);
});
}) ;
问题在于您试图访问嵌套
$ b
#re_contentIframe
框架嵌套在 #editorf
,然后嵌套在你的文档中。
试试:
<$ c $ ('#editorf')。contents()
$('#re_contentIframe')。
小提琴: http://jsfiddle.net/8VP4y/3/
I am using a hosted CMS that renders an iFrame within another iFrames. These iFrames are loaded from the same domain but since this is a hosted CMS I do not have access to these iFrames directly. Is it possible, using jQuery, to insert HTML content into the body
tag of the iFrame with the id of re_contentIframe
?
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.
Try:
$('#re_contentIframe').contents().find('body').find('#editorf').contents()
Fiddle: http://jsfiddle.net/8VP4y/3/
这篇关于使用jQuery将HTML插入到iFrame正文标签中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!