Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/437.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Can';t达到Iframe中div的高度_Javascript_Html_Iframe - Fatal编程技术网

Javascript Can';t达到Iframe中div的高度

Javascript Can';t达到Iframe中div的高度,javascript,html,iframe,Javascript,Html,Iframe,我的问题是: iFrame的大小必须根据其中div的大小而变化,当我尝试获取该div的大小时,我尝试的所有结果都返回0 var childiFrame=document.getElementById(“myDiv”); log(childiFrame,“这是我的iframe”); var innerDoc=childiFrame.contentWindow.document; log(innerDoc,“这是我必须找到div的innerDoc”); mdDiv=innerDoc.getEleme

我的问题是: iFrame的大小必须根据其中div的大小而变化,当我尝试获取该div的大小时,我尝试的所有结果都返回0

var childiFrame=document.getElementById(“myDiv”);
log(childiFrame,“这是我的iframe”);
var innerDoc=childiFrame.contentWindow.document;
log(innerDoc,“这是我必须找到div的innerDoc”);
mdDiv=innerDoc.getElementById(“md”);
log(mdDiv,“这是我的div!”);//我可以看到clientHeight=245
var divHeight=mdDiv.clientHeight;//或是看不见或是看不见
log(divHeight,“div高度始终等于0!”);

我没见过有人谈论这个问题,所以如果有人有任何想法,我就接受它

我认为您需要先使用
onload
事件等待iframe完全加载,然后尝试访问
mdDiv
属性,如:

var childiFrame = document.getElementById("myDiv");
console.log(childiFrame, "here is my iframe");
childiFrame.onload = function() {
  console.log('my iframe is loaded');

  var innerDoc = childiFrame.contentDocument ? childiFrame.contentDocument : childiFrame.contentWindow.document;
  console.log(innerDoc, "here is the innerDoc where i have to find my div");

  mdDiv = innerDoc.getElementById("md");
  console.log(mdDiv, "here is my div !");

  var divHeight = mdDiv.clientHeight; // or offsetHeight or scrollHeight
  console.log(divHeight);
};

请将此脚本放入父页面-


window.addEventListener('message',函数(e){
var data=e.data.split('-'),
滚动高度=数据[0],
iframe_id=数据[1];
如果(iframe_id=='iframe1')
document.getElementById('iframe-container-1')。style.height=scroll_height+'px';},false);

thx!我等待控制台。日志,它显示了良好的高度:)请考虑标记答案,以帮助未来的用户也。谢谢太棒了!