3 回答

TA貢獻(xiàn)1862條經(jīng)驗(yàn) 獲得超6個(gè)贊
先拿到你的irame
var iframe = document.getElementById('id_description_iframe');// orvar iframe = document.querySelector('#id_description_iframe');
然后使用jQuery的解決方案
var iframeDocument = iframe.contentDocument || iframe.contentWindow.document;
它甚至在InternetExplorer中工作,后者在 contentWindow
的屬性 iframe
對(duì)象。大多數(shù)其他瀏覽器使用 contentDocument
屬性,這就是為什么我們?cè)谶@個(gè)或條件下首先證明這個(gè)屬性的原因。如果沒有設(shè)置,請(qǐng)嘗試 contentWindow.document
.
在iframe中選擇元素
getElementById()
querySelectorAll()
iframeDocument
:
if (!iframeDocument) { throw "iframe couldn't be found in DOM.";}var iframeContent = iframeDocument.getElementById('frameBody'); // orvar iframeContent = iframeDocument.querySelectorAll('#frameBody');
調(diào)用iframe中的函數(shù)
window
iframe
jQuery
):
var iframeWindow = iframe.contentWindow;// you can even call jQuery or other frameworks/ / if it is loaded inside the iframeiframeContent = iframeWindow.jQuery('#frameBody');// oriframeContent = iframeWindow.$('#frameBody'); // or even use any other global variableiframeWindow.myVar = window.myVar; // or call a global functionvar myVar = iframeWindow.myFunction(param1 /*, ... */);
注

TA貢獻(xiàn)1860條經(jīng)驗(yàn) 獲得超8個(gè)贊
$("#id_description_iframe").contents().find("body").html()

TA貢獻(xiàn)1825條經(jīng)驗(yàn) 獲得超6個(gè)贊
document.getElementById('iframe_id').contentWindow.document.body.innerHTML;
添加回答
舉報(bào)