標籤:方法 alt tools body copyto attr type idt head
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();
用原生js在父頁面擷取iframe子頁面的元素,以及在子頁面擷取父頁面元素,這是平時經常會用到的方法,這裡寫一個例子來總結下:
1、父頁面(demo.html),在父頁面修改子頁面div的背景色為灰色,原來為紅色:
[html] view plain copy
- <!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=utf-8" />
- <title>demo首頁面</title>
- <script type="text/javascript">
- window.onload = function(){
- var _iframe = document.getElementById(‘iframeId‘).contentWindow;
- var _div =_iframe.document.getElementById(‘objId‘);
- _div.style.backgroundColor = ‘#ccc‘;
- }
-
- </script>
- </head>
-
- <body>
-
- <div id=‘parDiv‘>父頁面</div>
- <iframe src="demo-iframe.html" id="iframeId" height="150" width="150"></iframe>
- </body>
- </html>
2、子頁面(demo-iframe.html),在子頁面修改父頁面div的字型顏色為紅色,原來為黑色:
[html] view plain copy
- <!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=utf-8" />
- <title>子頁面demo13-iframe.html</title>
- <script type="text/javascript">
- window.onload = function(){
- var _iframe = window.parent;
- var _div =_iframe.document.getElementById(‘parDiv‘);
- _div.style.color = ‘red‘;
- }
- </script>
- </head>
-
- <body>
- <div id=‘objId‘ style=‘width:100px;height:100px;background-color:red;‘>子頁面</div>
- </body>
- </html>
3、:
(1)沒有加入js時的:
(2)加入js後的:
jquery、js調用iframe父視窗與子視窗元素的方法整理