父子頁面之間元素相互操作(iframe子頁面)

來源:互聯網
上載者:User

標籤:nload   測試   blank   element   jquer   src   html   tar   str   

js/jquery擷取iframe子頁面中元素的方法:

      一、使用window.frames["iframe的ID"]擷取元素   

window.onload = function() {        var oIframe = window.frames["oIframe"].document.getElementById("getFrame");        console.log(oIframe);}

       注意:此處一定要加上window.onload或者DOMContentLoaded,也就是DOM載入或者頁面載入完成後。

     二、使用window.name擷取元素   

var oIDframe = window.oIframe.document.getElementById("getFrame");console.log(oIDframe);
oIframe是iframe的name屬性值,這種擷取方法不必寫在window.onload或者DOMContentLoaded中,請自行測試。

三、使用getElementById擷取元素 
var oIdFrame = document.getElementById("oIframe").contentWindow.document.getElementById("getFrame");console.log(oIdFrame);

使用document.getElementById擷取本頁面的iframe元素後,再擷取iframe子頁面的元素。這種擷取方法不必寫在window.onload或者DOMContentLoaded中

 四、使用jquery擷取

var ojIframe = $("#oIframe").contents().find("#getFrame").html(); console.log(ojIframe);

js/jquery  iframe子頁面擷取父頁面元素的方法:

      一、使用js

var fatherEle = window.parent.document.getElementById("title"); console.log(fatherEle);

  二、使用jq

var fatherEleJq = $("#title",parent.document);console.log(fatherEleJq);

父頁面:

<div id="title">        index包含iframe子頁面    </div>    <div id="parent">        <iframe name="oIframe" id="oIframe" src="iframe.html" frameborder="0" width="1000" height="562"></iframe></div>

iframe.html子頁面:

<div id="getFrame">iframe</div>

參考連結:http://java-my-life.iteye.com/blog/1275205

父子頁面之間元素相互操作(iframe子頁面)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.