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); 
发布了97 篇原创文章 · 获赞 56 · 访问量 39万+

猜你喜欢

转载自blog.csdn.net/menghuanzhiming/article/details/103452454
今日推荐