天天看點

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

下面是内部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中的内容





繼續閱讀