jquery 如何实现iframe页面的切换??

jquery 如何实现iframe页面的切换??
我得main.html有两个iframe,left和center其中left.html里是菜单,点击菜单选项切换center 里的页面,我得菜单是通过tree实现的,tree的点击事件可以的到,但是点击事件里的内容怎么写??
$(function(){
$('#menuTree').tree({
url:'menuData.json',
onClick:function(node){
$(this).tree('toggle', node.target); //这儿怎么写??可以让center里切换到另一个页面?
},
});
});
满意再加分!谢谢了!

query取得iframe中元素的几种方法

在iframe子页面获取父页面元素
代码如下:

$('#objId', parent.document);
// 搞定...

在父页面 获取iframe子页面的元素
代码如下:

$("#objid",document.frames('iframename').document)

$(document.getElementById('iframeId').contentWindow.document.body).html()

显示iframe中body元素的内容。

$("#testId", document.frames("iframename").document).html();

根据iframename取得其中ID为"testId"元素

$(window.frames["iframeName"].document).find("#testId").html()

用JS或jQuery访问页面内的iframe,兼容IE/FF
注意:框架内的页面是不能跨域的!

假设有两个页面,在相同域下.

index.html 文件内含有一个iframe:

XML/HTML代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>页面首页</title>
</head>

<body>
<iframe src="iframe.html" id="koyoz" height="0" width="0"></iframe>
</body>
</html>
iframe.html 内容:

XML/HTML代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>iframe.html</title>
</head>

<body>
<div id="test">www.koyoz.com</div>
</body>
</html>

1. 在index.html执行JS直接访问:

JavaScript代码
document.getElementById('koyoz').contentWindow.document.getElementById

('test').style.color='red'
通过在index.html访问ID名为'koyoz'的iframe页面,并取得此iframe页面内的ID为'test'的

对象,并将其颜色设置为红色.

此代码已经测试通过,能支持IE/firefox .

2. 在index.html里面借助jQuery访问:

JavaScript代码
$("#koyoz").contents().find("#test").css('color','red');
此代码的效果和JS直接访问是一样的,由于借助于jQuery框架,代码就更短了.

收集网上的一些示例:
用jQuery在IFRAME里取得父窗口的某个元素的值
只好用DOM方法与jquery方法结合的方式实现了

1. 在父窗口中操作 选中IFRAME中的所有单选钮
$(window.frames["iframe1"].document).find("input:radio").attr("checked","true");

2. 在IFRAME中操作 选中父窗口中的所有单选钮
$(window.parent.document).find("input:radio").attr("checked","true");

父窗口想获得IFrame中的Iframe,就再加一个frames子级就行了,如:
$(window.frames["iframe1"].frames["iframe2"].document).find("input:radio").attr("checked","true");
温馨提示:内容为网友见解,仅供参考
第1个回答  推荐于2016-07-08
Jquery弹出层窗口,并且背景变暗
先到 下载Jquery的Lee dialog插件,
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script language="javascript" src="dialog.js"></script>
<script language="javascript" src="Jquery.js"></script>
<style>
#floatBoxBg
.floatBox
.floatBox .title
.floatBox .title h4
.floatBox .title span
.floatBox .content
</style>
</head>

<body>
<script>
function aa()
{
dialog("我的标题","iframe:login.php","500px","auto","text"); // login.php 为要弹出的窗口
}

</script>
<a href="#" onclick="aa()" >Click Here</a>
</body>
</html>

关于这个dialog插件的其它用法:
get加载一个.html文件(也可以是.php/.asp?id=4之类的)
dialog("我的标题","test.html","200px","auto","text");

弹出纯文本内容
dialog("我的标题","text:我的内容","200px","auto","text"); 试试
弹出某id里面的html
页面有<div id="testID" style="display:none;"><h2>Lee dialog</h2></div>
dialog("我的标题","id:testID","300px","auto","id"); 试试
Lee dialog
加载一个页面以框架示
把blueidea加载进来,定义css:body .iframe .content复盖一下,因为.content默认padding:20px;
dialog("blueidea","iframe:","500px","500px","iframe");

本回答被提问者和网友采纳
第2个回答  2011-09-22
因我不知道你用的tree插件是什么样的,方便的话,给个地址;
我就自己弄了些测试代码。具体如下:
----main.html----
<iframe id="left" width="100" height="300" src="iframe-left.html"></iframe>

<iframe id="center" width="800" height="300" src=""></iframe>
----left.html----
<a href="javascript:void(0)" rel="http://www.baidu.com/">baidu</a>
<a href="javascript:void(0)" rel="http://www.google.com/">google</a>
<script type="text/javascript">
$(document).ready(function(){
$('a').click(function(){
$(window.parent.document).find("#center").attr("src",$(this).attr("rel"));
});
});
</script>

我在本地测试是可以顺利切换center中的页面的。
原理是一样的,你需要控制由tree生成的链接指向到center中去就可以了。
这里我将node.target理解为是框架集中目标iframe的name值。
结合你的需求,你那儿可能需要这样写:
$(window.parent.document).find("iframe[name='"+node.target+"']").attr("src",node.attributes.url);
第3个回答  推荐于2018-04-08
比如说你菜单是个链接<a href="地址" target="center">某某</a>
如果写方法也一致,document.parent.center.location="地址"
第4个回答  2015-09-24
$('iframe').attr("src",url);//更改访问路径
相似回答