iframe頁面和父頁面之間元素查找的方法

從父頁面中查找iframe子頁面中對象的方法:

JS:

    document.getElementById('iframe').contentWindow //查找iframe加載的頁面的window對象  
    document.getElementById('iframe').contentWindow.document //查找iframe加載的頁面的document對象  
    document.getElementById('iframe').contentWindow.document.body //查找iframe加載的頁面的body對象  
    document.getElementById('iframe').contentWindow.document.getElementById('icontent') //查找iframe加載的頁面的id爲icontent的對象  

jQuery:

$('#iframe').contents() //查找iframe加載的頁面的document對象  
$('#iframe').contents().find('body') //查找iframe加載的頁面的body對象  
$('#iframe').contents().find('body').find('#icontent') //查找iframe加載的頁面的id爲icontent的對象 

從iframe中查找父頁面中對象的方法:

JS:

    [window.]parent //查找父頁面的window對象  
    [window.]parent.document //查找父頁面的document對象  
    [window.]parent.document.body //查找父頁面的body對象  
    [window.]parent.document.getElementById('button') //查找父頁面中id爲button的對象  

jQuery:

<span style="font-family:Arial;font-size:18px;">$([window.]parent) //查找父頁面的window對象  
$([window.]parent.document) //查找父頁面的document對象  
$([window.]parent.document.body) //查找父頁面的body對象  
$([window.]parent.document.body).find('#button') //查找父頁面中id爲button的對象  </span>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章