在IFrame中查找IFRAME中的元素的方式

    xiaoxiao2023-06-20  166

    下面是内部iframe找外部mainFrame的情况  var websiteSearchButton = window.parent.parent.document.getElementById('mainFrame')      .contentWindow.document.getElementById("webresource-search-button");  iframe中 1、子页面找符页面中的元素 $(window.parent.document).find(id); 2、父页面--->子页面中的 $(id).contents().find(元素id).find(元素id); 3、子页面--->爷爷界别的页面 $(window.top.document).find(id); 例子: var body2 = jQuery(window.top.document.getElementById("eWebEditor2")).eq(0).contents().find("#eWebEditor").contents().find("body").html(); console.log(body2); 1、window.top.document.getElementById("eWebEditor2")     :表示在一个iframe中找最顶层中的document,然后在找最外层的document中的eWebEditor2的这个元素(上例子表示的是一个iframe), 2、jQuery(window.top.document.getElementById("eWebEditor2"))    表示把eWebEditor2这个iframe转成jQuery对象 3、jQuery(window.top.document.getElementById("eWebEditor2")).eq(0)   表示取到第一个元素 4、.contents().find("#eWebEditor")    表示取eWebEditor2这个iframe中eWebEditor这个元素(这里这个元素表示的又是一个iframe) 5、jQuery(window.top.document.getElementById("eWebEditor2")).eq(0).contents().find("#eWebEditor").contents()    表示eWebEditor这个iframe中的内容 6、jQuery(window.top.document.getElementById("eWebEditor2")).eq(0).contents().find("#eWebEditor").contents().find("body")   表示取到eWebEditor这个内容中的body元素 7、最终 jQuery(window.top.document.getElementById("eWebEditor2")).eq(0).contents().find("#eWebEditor").contents().find("body").html();    表示body中的内容  
    最新回复(0)