js获取iframe中的元素以及在iframe中获取父级的元素(包括iframe中不存在name和id的情况)
var ifr_document = iframe.contentWindow.document;//iframe中的文档内容
- var frameWin=document.getElementById(‘iframe‘).contentWindow; //window对象
- var frameDoc=document.getElementById(‘iframeId‘).contentWindow.document //document对象
- var frameBody=document.getElementById(‘iframeId‘).contentWindow.document.body //body对
- a、 用contentWindow方法
- document.getElementById(‘iframe1‘).onload=function(){
- this.contentWindow.run();
- }
- b、用iframes[]框架集数组方法
- document.getElementById(‘iframe1‘).onload=function(){
- frames["iframe1"].run();
- }
- window.parent.attributeName; // 访问属性attributeName是在父级窗口的属性名
- window.parent.Func(); // 访问属性Func()是在父级窗口的方法
- $(‘#iframeId‘).load(function() { //方法1
- var iframeHeight = Math.min(iframe.contentWindow.window.document.documentElement.scrollHeight, iframe.contentWindow.window.document.body.scrollHeight);
- var h=$(this).contents().height();
- $(this).height(h+‘px‘);
- });
- $(‘#iframeId‘).load(function() { //方法2
- var iframeHeight=$(this).contents().height();
- $(this).height(iframeHeight+‘px‘);
- });
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。