jquery、js父子页面操作总结
jquery 父、子頁面之間頁面元素的獲取,方法的調用
https://www.cnblogs.com/it-xcn/p/5896231.html
一、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
========
jquery 獲取父窗口的元素、父窗口、子窗口
https://www.cnblogs.com/goloving/p/7642381.html
一、獲取父窗口元素:
$("#父窗口元素ID",window.parent.document);對應javascript版本為window.parent.document.getElementById("父窗口元
素ID");
取父窗口的元素方法:$(selector, window.parent.document);
那么你取父窗口的父窗口的元素就可以用:$(selector, window.parent.parent.document);
類似的,取其它窗口的方法大同小異
$(selector, window.top.document);
$(selector, window.opener.document);
$(selector, window.top.frames[0].document);
二、Javascript彈出子窗口
可以通過多種方式實現,下面介紹幾種方法
(1) 通過window對象的open()方法,open()方法將會產生一個新的window窗口對象
其用法為:window.open(URL,windowName,parameters);
URL: 描述要打開的窗口的URL地址,如何為空則不打開任何網頁;
windowName:描述被打開的窗口的名稱,可以使用'_top'、'_blank'等內建名稱,這里的名稱跟<a href="..." target="...">
里的target屬性是一樣的。
parameters:描述被打開的窗口的參數值,或者說是樣貌,其包括窗口的各個屬性值,及要傳入的參數值。
例如:
//打開一個400 x 100 的干凈的窗口:
open
('','_blank','width=400,height=100,menubar=no,toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resiz
able=yes')
//也可以這樣寫:
var newWindow = open('','_blank');
參數說明如下:
top=# 窗口頂部離開屏幕頂部的像素數
left=# 窗口左端離開屏幕左端的像素數
width=# 窗口的寬度
height=# 窗口的高度
menubar=... 窗口有沒有菜單,取值yes或no
toolbar=... 窗口有沒有工具條,取值yes或no
location=... 窗口有沒有地址欄,取值yes或no
directories=... 窗口有沒有連接區,取值yes或no
scrollbars=... 窗口有沒有滾動條,取值yes或no
status=... 窗口有沒有狀態欄,取值yes或no
resizable=... 窗口給不給調整大小,取值yes或no
(2) 在javascript中除了通過open()方法建立window對象實現彈出窗口外,還可以通過建立對話框的方式彈出窗口。如:
alert(""); //彈出信息提示對話框
confirm(""); //彈出信息確認對話框
prompt(""); //具有交互性質的對話框
但是,上述實現的彈出窗口具有的功能較為單一,只能完成較為簡單的功能。對于需要在對話框中顯示多個數據信息,甚至是
HTML控件就無能為力了。
(3) 使用模態對話框實現復雜的對話框需求
在javascript的內建方法中還有一類方法可以實現通過對話框顯示HTML內容,也就是說可以通過創建對話框的方式來完成創建
窗口對象所能完成的功能。
包括創建模態對話框和非模態對話框兩種。
? 實現方法為:?
//創建模態你對話框
window.showModalDialog(sURL,vArguments,sFeatures)
//創建非模態對話框
window.showModelessDialog(sURL,vArguments,sFeatures)
其區別在于:用showModelessDialog()打開窗口時,不必用window.close()去關閉它,當以非模態方式[IE5]打開時,打開
對話框的窗口仍可以進行其他的操作,即對話框不總是最上面的焦點,當打開它的窗口URL改變時,它自動關閉。而模態[IE4]方
式的對話框始終有焦點(焦點不可移走,直到它關閉)。模態對話框和打開它的窗口相聯系,因此我們打開另外的窗口時,他們的
鏈接關系依然保存,并且隱藏在活動窗口的下面。showModeDialog()則不然。
參數說明:
sURL:必選參數,類型:字符串。用來指定對話框要顯示的文檔的URL
vArguments:可選參數,類型:變體。用來向對話框傳遞參數。傳遞的參數類型不限,包括數組等。對話框通過
window.dialogArguments來取得傳遞進來的參數
sFeatures:選參數,類型:字符串。用來描述對話框的外觀等信息,可以使用以下的一個或幾個,用分號“;”隔開
dialogHeight:對話框高度,不小于100px,IE4中dialogHeight和dialogWidth 默認的單位是em,而IE5中是px,為方便其見,在
定義modal方式的對話框時,用px做單位
dialogWidth: 對話框寬度
dialogLeft: 距離桌面左的距離
dialogTop: 離桌面上的距離
center: 窗口是否居中,默認yes,但仍可以指定高度和寬度,取值范圍{yes | no | 1 | 0 }
help: 是否顯示幫助按鈕,默認yes,取值范圍{yes | no | 1 | 0 }
resizable: 是否可被改變大小,默認no,取值范圍{yes | no | 1 | 0 } [IE5+]
status: 是否顯示狀態欄,默認為yes[ Modeless]或no[Modal],取值范圍{yes | no | 1 | 0 } [IE5+]
scroll:指明對話框是否顯示滾動條,默認為yes,取值范圍{ yes | no | 1 | 0 | on | off }
還有幾個屬性是用在HTA中的,在一般的網頁中一般不使用。
dialogHide:在打印或者打印預覽時對話框是否隱藏,默認為no,取值范圍{ yes | no | 1 | 0 | on | off }。
edge:指明對話框的邊框樣式,默認為raised,取值范圍{ sunken | raised }。
unadorned:默認為no,取值范圍{ yes | no | 1 | 0 | on | off }。
?
傳入參數:要向對話框傳遞參數,是通過vArguments來進行傳遞的。類型不限制,對于字符串類型,最大為4096個字符。也可
以傳遞對象
var newWin=window.showModalDialog(url,window,'dialogHeight:500px, dialogLeft:100px,
dialogTop:100px,dialogWidth:300px, status:0, edge:sunken');
newWin.open();
與使用window.open()方法創建窗口相比,模態方法創建窗口的區別在于有模態方法創建的窗口后將不能操作父窗口。
三、子窗口與父窗口間通信:
(1) 使用window.open()創建的窗口與父窗口通信
可以在子窗口頁面中通過window.opener來獲取父窗口對象,獲取之后子窗口便可以對父窗口執行刷新,傳值等操作。
window.opener.location.reload(); //子窗口刷新父窗口
window.opener.location.href //獲取父窗口href
window.opener.locaiton.pathname //獲取父窗口路徑名
//刷新父頁面
window.location.href=window.location.href ; //重新定位父頁面
window.location.reload;
(2) 模態窗口與父窗口通信
通過使用showModelDialog() 及showModelessDialog() 方法創建的子窗口想與父窗口通信時,不能通過window.opener來獲取
父窗口對象。
要實現通信,必須在創建模態子窗口時向子窗口傳入父窗口對象。
? (一)實現方式為:
1、在父窗口中:
var newWin=window.showModelDialog(url,window,'');
newWin.open();
此時參數window即是父窗口對象
2、在子窗口中:
需首先獲取父窗口對象,然后才能使用父窗口對象。由于父窗口對象是在創建子窗口時通過傳入參數的方式傳入的,因此,在
子窗口中也只能通過獲取窗口參數的方式獲取父窗口對象。獲取方式如下:
var parent=widnow.dialogArguments;
變量parent便是父窗口對象。
//通過子窗口提交父窗口中的表單:form1,提交后執行查詢操作
var parent=window.dialogArguments;
parent.document.form1.action="QueryInfor.jsp";
parent.submit();
//刷新父頁面
var parent=window.dialogArguments;
parent.location.reload();
從子窗口傳值到父窗口:要實現在模態子窗口中傳值到父窗口,需要使用window.returnValue完成
(二)實現方法如下:
1、在子窗口中:
//獲取父窗口某字段值,對該值加一后返回父窗口
var parent=window.dialogArguments;//獲取父窗口對象
var x=parent.docuement.getElementById("age").value;
x=x+1;
?
//傳回x值
window.returnValue=x;
2、在父窗口中:
//獲取來自子窗口的值
var newWin=window.showModelDialog(url,window,'');
if(newWin!=null){
? ? document.getElementById("age").value=newWin;
}
(三)在子窗口中設置父窗口的值
在子窗口中向父窗口中傳入值似乎沒有直接設置父窗口中的值來得明了。直接設置父窗口中元素的值顯得要更靈活一些,不過
具體使用哪種方法要根據實際情況和已有的實現方式而定,因為如果使用了不切實際的方法不僅降低開發效率,也降低了執行效率
,往往也會造成不優雅的實現方式和代碼風格。
子窗口設置父窗口的值使用方法如下:
1、子窗口中:
var parent=window.dialogArguments;
var x=parent.document.getElementById("age").value;
x=x+1;
2、設置父窗口中age屬性值
parent.document.getElementById("age").value=x;
以上是我在項目中使用javascript解決子窗口問題時,收集及積累的一些方法和資料。其實不論是使用window.open()還是使
用window.showModelDialog()進行傳參等操作時,雖然在實現方法上有很大的差別,初次接觸會覺得有點亂,但只要理清子窗口與
父窗口之間的關系和角色之后,就很好理解了。
========
利用JQuery操作iframe父頁面、子頁面的元素和方法匯總
https://www.jb51.net/article/123314.htm
主要給大家介紹了關于利用JQuery操作iframe父頁面、子頁面的元素和方法的相關資料,
iframe在復合文檔中經常用到,利用jquery操作iframe可以大幅提高效率,本文主要給大家分享了關于簡單使用JQUERY來操作
IFRAME的一些記錄,這個使用純JS也可以實現。
第一、在iframe中查找父頁面元素的方法:
?
$('#id', window.parent.document)
?
第二、在父頁面中獲取iframe中的元素方法:
?
$(this).contents().find("#suggestBox")
?
第三、在iframe中調用父頁面中定義的方法和變量:
parent.method parent.value
?
JQUERY、JS調用IFRAME父窗口與子窗口元素的方法
1. jquery 在iframe子頁面獲取父頁面元素代碼如下:
$("#objid", parent.document)
?
2. jquery在父頁面 獲取iframe子頁面的元素 代碼如下:
?
$("#objid",document.frames('iframename').document)
?
3.js 在iframe子頁面獲取父頁面元素代碼如下:
indow.parent.document.getElementByIdx_x("元素id");
?
4.js 在父頁面獲取iframe子頁面元素代碼如下:
window.frames["iframe_ID"].document.getElementByIdx_x("元素id");?
5.子類iframe內調用父類函數:
window.parent.func();
?
========
jquery 父子頁面傳參的解決方案
https://blog.csdn.net/zxmin1302/article/details/83623023
父頁面相關:
按鈕:
<button class="btn tab btn-default" value="1" id="tz" style="height: 35px;width: 135px;">跳轉</button>
js邏輯:
$('#tz').on('click',function(){
? ? ? ? window.open('xxxx?data=1', '_blank', 'height=900, width=1000, top=0, left=0, toolbar=no, menubar=no,
scrollbars=no, resizable=no, location=no, status=no');
});
function returnDataParam(data){
? ? console.log(data);
? ? return data;
}
獲取子頁面的url參數
//獲取url參數
function GetQueryString(name) {
? ? var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
? ? var r = window.location.search.substr(1).match(reg);
? ? if(r!=null)return ?unescape(r[2]); return null;
}
子頁面傳參:
var data = GetQueryString(data);
if(data==1){
? ? var dataParam = {
? ? ? ?url : 'xxxx',
? ? ? ?indicators :'xxxx',
? ? ? ?conditions : 'xxxx'
? ? ?}
? ? ?//條件篩選整理成相關的字符串
? ? ? ? ? ?
? ? //獲取數據參數
? ? alert('數據提取成功!');
? ? window.opener.returnDataParam(dataParam);
? ? //關閉當前窗口
? ? ?window.close();
} ? ? ? ? ? ? ? ? ? ?
?
========
JS 和 JQuery 對 Iframe 內外(父子)頁面進行操作
https://blog.csdn.net/andseny/article/details/53138509
在web開發中,經常會用到iframe,難免會碰到需要在父窗口中使用iframe中的元素、或者在iframe框架中使用父窗口的元素
在父窗口中獲取iframe中的元素
1、格式:window.frames["iframe的name值"].document.getElementByIdx_x("iframe中控件的ID").click();
? ? ?實例:window.frames["ifm"].document.getElementByIdx_x("btnOk").click();
2、var obj=document.getElementByIdx_x("iframe的name").contentWindow;
? ? ?var ifmObj=obj.document.getElementByIdx_x("iframe中控件的ID");
? ? ? fmObj.click();
實例:var obj=document.getElementByIdx_x("ifm").contentWindow;
? ? ? ? ? var ifmObj=obj.document.getElementByIdx_x("btnOk");
? ? ? ? ? ifmObj.click();
在iframe中獲取父窗口的元素
格式:window.parent.document.getElementByIdx_x("父窗口的元素ID").click();
實例:window.parent.document.getElementByIdx_x("btnOk").click();
jquery
在父窗口中獲取iframe中的元素
1、格式:$("#iframe的ID").contents().find("#iframe中的控件ID").click();//jquery 方法1
? ? ?實例:$("#ifm").contents().find("#btnOk").click();//jquery 方法1
2、格式:$("#iframe中的控件ID",document.frames("frame的name").document).click();//jquery 方法2
? ? ?實例:$("#btnOk",document.frames("ifm").document).click();//jquery 方法2
在iframe中獲取父窗口的元素
格式:$('#父窗口中的元素ID', parent.document).click();
實例:$('#btnOk', parent.document).click();
新增方法:
1.js在iframe子頁面操作父頁面元素代碼:
window.parent.document.getElementByIdx_x("父頁面元素id");
2.js在父頁面獲取iframe子頁面元素代碼如下:
window.frames["iframe_ID"].document.getElementByIdx_x("子頁面元素id");
3. jquery在iframe子頁面獲取父頁面元素代碼如下:
$("#objid",parent.document)
4. jquery在父頁面獲取iframe子頁面的元素·
$("#objid",document.frames('iframename').document)
5.在iframe中調用父頁面中定義的方法和變量:
window.parent.window.parentMethod();
window.parent.window.parentValue;
6.在父頁面操作iframe子頁面的方法和變量
//code from http://caibaojian.com/js-get-iframe.html
window.frames["iframe_ID"].window.childMethod();
window.frames["iframe_ID"].window.childValue;
========
jquery_iframe父子級頁面事件使用
https://www.cnblogs.com/sugege/p/7902766.html
1、index.html
<body topmargin='0' leftmargin='0' style="background:#ccc"> //這里的屬性只是為了全屏下邊無間隙
index.html
? ? <iframe src="main.html" id="indexMain" style="margin-bottom:-5px;position:relative;"></iframe> //這里的
樣式的為了跳轉之后移動端input輸入完成之后下邊無間隙
</body>
<script>
/*父級本頁面事件*/
? ? function oIndexFn(){
? ? ? ? ? ? alert("父級頁面index.html事件調用成功");
? ? ? ? }
/*調用子頁面事件*/
$(document).click(function(){
alert("您點擊的是父頁面[index]")
? ? $("#indexMain")[0].contentWindow.oMainFn(); ? ?//iframe的id
})
</script>
2、main.html
<body style="background:#f0f0f0">
? ? main.html
</body>
<script>
/*子級本頁面事件*/
? ? function oMainFn(){
? ? ? ? ? ? alert("子頁面main.html事件調用成功");
? ? ? ? }
/*調用父頁面事件*/
$(document).click(function(){
alert("您點擊的是子頁面![main]")
? ? parent.window.oIndexFn(); ? ? ? //調用父級頁面事件
})
</script> ? ?
========
?
總結
以上是生活随笔為你收集整理的jquery、js父子页面操作总结的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Sql Server 2008 精简版(
- 下一篇: 使用Notepad++ 删除非连续的列内