2012-04-12 71 views
2

我尝试访问由jquery colorbox插件生成的iframe的内容几个小时没有成功。使用jquery访问colorbox Iframe内容

继承人我的代码:

$(".ListView li a").colorbox({ 

iframe:true, 
width: "50%", 
height: "50%", 
opacity: 0.5, 
onComplete: function() { 
alert("complete"); 
$("iframe").contents().find("p").css("color","#f00"); 
} 
}); 

它加载一个文件并显示它。如此精细。当iframe加载完成时,还会引发警报消息。但我无法通过jquery选择器访问框架内的元素。 iframe中的所有p元素都不会发生任何变化。

无论

if($("iframe").contents().length > 0) 
{ 
alert("exists");  
} 

也不

if($("iframe").length > 0) 
{ 
alert("exists");  
} 

是成功的。页面上没有其他iframe,我也尝试插入类名称。

对于“正常”的iframe,直接插入到html中而不是由colorbox生成,我的函数正在工作。

谢谢产生

代码:

<div id="colorbox" class="" style="display: block; padding-bottom: 50px; padding-right: 50px; top: 128px; left: 476px; position: absolute; width: 902px; height: 207px;"> 
<div id="cboxWrapper" style="height: 257px; width: 952px;"> 
<div> 
<div style="clear: left;"> 
<div id="cboxMiddleLeft" style="float: left; height: 207px;"></div> 
<div id="cboxContent" style="float: left; width: 902px; height: 207px;"> 
<div id="cboxLoadedContent" style="display: block; width: 902px; overflow: auto; height: 187px;"> 
<iframe class="cboxIframe" frameborder="0" name="cbox1334265249803" src="qtest.html"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<body> 
<a href="#">Link</a> 
<p>Formatted Text</p> 
</body> 
</html> 
</iframe> 
</div> 
<div id="cboxLoadingOverlay" style="float: left; display: none;"></div> 
<div id="cboxLoadingGraphic" style="float: left; display: none;"></div> 
<div id="cboxTitle" style="float: left; display: block;"></div> 
<div id="cboxCurrent" style="float: left; display: none;"></div> 
<div id="cboxNext" style="float: left; display: none;"></div> 
<div id="cboxPrevious" style="float: left; display: none;"></div> 
<div id="cboxSlideshow" style="float: left; display: none;"></div> 
<div id="cboxClose" style="float: left;">close</div> 
</div> 
<div id="cboxMiddleRight" style="float: left; height: 207px;"></div> 
</div> 
<div style="clear: left;"> 
</div> 
<div style="position: absolute; width: 9999px; visibility: hidden; display: none;"></div> 
</div> 
<div class="ui-dialog ui-widget ui-widget-content ui-corner-all ui-draggable" style="display: none; z-index: 1000; outline: 0px none;" tabindex="-1" role="dialog" aria-labelledby="ui-dialog-title-box-trash-dialog"> 

回答

3

使用iframe时,只要将iframe元素添加到文档中,onComplete实际上会触发。如果要查询内容,则必须等待DOM准备就绪。父文档无法知道iframe的DOM何时准备就绪,它只能监听窗口的onload事件。

但是,您可以通过将fastIframe属性设置为false来触发colorbox等待并触发iframe的onload事件触发onComplete函数。例如:

$('a.example').colorbox({iframe:true, fastIframe:false, width:500, height:500}) 

当然,这一切假设的iframe从同一个域父始发,并且没有安全限制导致您无法访问iframe的内容。

+0

非常感谢! Google搜索“完整的colorbox”后,我发现这个网页:http://groups.google。com/group/colorbox/browse_thread/thread/77e89cc514d43abf 你在哪里解释给别人。现在它真棒。 – simonheinrich 2012-04-14 17:15:05

0

有一个绍兴德胜浏览器解决方案来访问一个iframe的内容,你可以读到它here

另外,你可以通过jQuery访问你的iFrame的内容,阅读更多here

这两种解决方案都可以通过ID找到您的iframe,因此您需要一个id来使用这些解决方案。我希望这有帮助。

+1

是的,我知道如何访问IFRAME。当我将