Js/Jquery獲取iframe中的元素(window、document)

參考地址:
Window frames 屬性:https://www.runoob.com/jsref/prop-win-frames.html
jQuery 遍歷 - contents() 方法:https://www.w3school.com.cn/jquery/traversing_contents.asp

  1. 獲取iframe中的window對象:
//js
document.getElementsByTagName('iframe')[0].contentWindow;
//js
//window.frames屬性返回窗口中所有命名的框架,該集合是 Window 對象的數組
window.frames[0];

//jquery
$('iframe')[0].contentWindow;
  1. 獲取iframe中的document對象:
//js
document.getElementsByTagName('iframe')[0].contentWindow.document;
//jquery
$('iframe').contents()[0];
  1. 在iframe中獲取父窗口的元素:
//js
格式:window.parent.document.getElementById("父窗口的元素ID"); 
實例:window.parent.document.getElementById("btnOk"); 

//jquery
格式:$('#父窗口中的元素ID', window.parent.document); 
實例:$('#btnOk', window.parent.document); 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章