window.parent 是iframe页面调用父页面对象,当我们想从iframe内嵌的页面中访问外层页面是可以直接利用window.parent获取;
一、jquery 父、子页面之间页面元素的获取,方法的调用
1. 父页面获取子页面元素:
格式:$("#iframe的ID").contents().find("#iframe中的控件ID").click();
实例:$("#ifm").contents().find("#iBtnOk").click(); // ifm 为 <iframe> 标签 id; iBtnOk 为子页面按钮 id
2. 父页面调用子页面方法:
格式:$("#iframe的ID")[0].contentWindow.iframe方法();
实例:$("#ifm")[0].contentWindow().iClick(); // ifm 为 <iframe> 标签 id; iClick为子页面 js 方法
3. 子页面获取父页面元素:
格式:$("#父页面元素id" , parent.document);
实例:$("#pBtnOk" , parent.document).click(); // pBtnOk 为父页面标签 id
二、原生 js 父页面元素的获取,方法的调用:
1. 子页面调用父页面方法:
格式:parent.父页面方法
实例:parent.pClick(); // pClick 为父页面 js 方法
2. 子页面获取父页面元素:
格式:window.parent.document.getElementById("父窗口元素ID");
实例:window.parent.document.getElementById("pBtnOk");// pBtnOk为父页面标签 id
下面的时从网上抄的。还没测试,不过感觉应该是对的。
function callIframeMethod() {
// js
document.getElementById("frame").contentWindow.test();
// jq
$("#frame")[0].contentWindow.test(); //用jquery调用需要加一个[0]
}
function callIframeField() {
// 以下两种方法可以达到同样的效果
console.log($("#frame")[0].contentWindow.ff);
console.log(frame.window.ff);
}
function callIframeHtml() {
// 以下两种方法可以达到同样的效果
console.log($("#frame")[0].contentWindow.$("#dd").val());
console.log(frame.window.$("#dd").val());
var t = document.getElementById('frame').contentWindow.document.getElementById('dd');
console.log(t);
// var t = document.getElementById('frame').contentWindow.document.getElementById('dd');
//console.log($("#frame")[0].contentWindow.document.getElementById("dd").value);
//console.log($("#frame")[0].contentWindow.document.getElementById("dd").value);
}
function giveParameter() {
$("#frame")[0].contentWindow.hellobaby="dsafdsafsdafsdafsdafsdafsadfsadfsdafsadfdsaffdsaaaaaaaaaaaaa";
}
下面从网上摘抄
“window.location.href”、”location.href”是本页面跳转
“parent.location.href”是上一层页面跳转
“top.location.href”是最外层的页面跳转
举例说明:
如果A,B,C,D都是jsp,D是C的iframe,C是B的iframe,B是A的iframe,如果D中js这样写
“window.location.href”、”location.href”:D页面跳转
“parent.location.href”:C页面跳转
“top.location.href”:A页面跳转
现在终于明白了连接的时候target的用法了:
_blank:重新打开一个窗口
_parent:父窗口执行重定向
_self:自身页面重定向
_top:第一个父窗口重定向
综上所述可知:parent.window:父窗口对象 top.window:第一个父窗口的对象
下面来重点看看window.parent与window.openner区别
window.parent 是iframe页面调用父页面对象,当我们想从iframe内嵌的页面中访问外层页面是可以直接利用window.parent获取
这个链接里面写的东西应该也不错,等周末看看。