繁体   English   中英

将iframe高度设置为其来源中的div ID之一

[英]set iframe height same as one of the div id in its source

我有一个文件为index_scorm.html。 几行代码如下:

<div class="cpMainContainer" id="cpDocument" style="left: 0px; top: 0px; height: 627px; background-color: rgb(255, 255, 255);"> 
    <div id="main_container" style="top: 0px; position: absolute; width: 100%; height: 627px; left: 0px;">
       <div id="projectBorder" style="top:0px;left:0px;width:100%;height:100%;position:absolute;display:block">
       </div>
    </div>
</div>

我称这个index_scorm.html文件为index.html文件中的iframe。 我希望此iframe的高度与id =“ cpDocument”的div的高度相同。 我不能简单地使用height属性将iframe高度设置为627px; 因为“ cpDocument”的高度会随着不同的屏幕尺寸而不断变化。 我想使用javascript实现此目的。

的HTML

//On load of iframe 'onLoadHandler' function will be called
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<iframe id="my-frame" src='143/safety_starter/index_scorm.html' onload="onLoadHandler();"></iframe>

JS

<script language="javascript" type="text/javascript">
  function onLoadHandler() {
    var iframe = jQuery('#my-frame').contents();
    var _height = iframe.find('#cpDocument').height() + 'px';
    jQuery('#my-frame').css('height',_height);
  }
</script>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM