好的,情况是这样的。我订阅了一个网站,可以让您添加自己的代码等。他们有一个论坛编辑器,我无法设置皮肤以匹配我的网站,所以我只想更改最内框架的颜色(以下示例中的 doc3)。
这是基本设置...是的,所有文档都来自同一域,但我只能将代码添加到主文档中。 doc3 框架是动态创建的。第一个框架有一个类,但没有名称,第二个框架只有一个 id...我不知道绑定是否适用于内部框架,但 firebug 没有给我任何错误。
哦,我也尝试过注入样式表,但没有成功。
主要文件(我尝试访问 doc3)
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('iframe').bind('load', function(){
$(this).contents().find('body').css({'background-color':'#333333','color':'#ddd'}); // This does change doc2 colors
$(this).contents().find('iframe#doc3').bind('load', function(){
$(this).contents().find('body').css({'background-color':'#333333','color':'#ddd'}); // doesn't work :(
})
})
})
</script>
</head>
<body>
Document #1
<iframe class="postFrame" src="doc2.htm" width="100%" height="300">
</body>
</html>
doc2.htm
<html>
<head>
</head>
<body>
<form id="form1">
Document #2
<iframe id="doc3" src="doc3.htm" width="100%" height="250">
</form>
</body>
</html>
doc3.htm
<html>
<head>
</head>
<body style="background-color:#fff; color:#000;"> <!-- access this body style -->
Document #3
</body>
</html>
我希望我已经说得足够清楚了。任何帮助或正确方向的观点将不胜感激:)
编辑:根据 Wahnfrieden 的建议更新了主文档(谢谢!),但遗憾的是我仍然无法访问 doc3.htm
假设您的 iframe 都在同一个域中,请尝试一下:
$(function() {
$(window).load(function() {
var iframe2body = $('iframe').contents().find('body');
iframe2body.css({ 'background-color': '#333333', 'color': '#ddd' }); // doc2 colors
iframe2body.contents('iframe').contents().find('body').css({ 'background-color': '#fff', 'color': '#ddd' }); // doc3 colors
})
})
我没有纯粹为了可读性而将其全部链接在一起,对于 IE,我必须将其更改为$(window).load(function() {
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)