javascript - 如何在 iframe 元素中获取动态添加的 body 元素
问题描述
在 iframe 元素中,我添加了一个 src 属性,当它加载时,将调用该函数,在这种情况下 document.body 指的是主体。
我已经尝试过使用$(document).ready(function() {})
and document.body[1]
,但没有任何成功。
<iframe id="inlineFrameExample" class="iframeDynamic" title="Inline Frame Example" width="300" name="myFrame" src="http://www.nu.nl" onload="resizeIframe(this)">
<html>
<head></head>
<body></body>
</html>
function resizeIframe() {
let test = document.body;
console.log(test);
// What's the page height?
var height = document.body.scrollHeight;
// Going to 'pipe' the data to the parent through the helpframe..
var pipe = document.getElementById('inlineFrameExample');
// Cachebuster a precaution here to stop browser caching interfering
newSource = pipe.src + '?height='+height+'&cacheb='+Math.random();
pipe.style.height = height + 'px';
}
在我看来, document.body 不会引用正确的 body 元素。还要记住,这是一个动态添加的元素。我想选择它,然后在 iframe 中获取该 body 元素的特定高度。
解决方案
推荐阅读
- java - 从firebase数据库循环抛出子节点并存储到RecyclerView
- docker - Docker 19.03.12:swarm 在 swarm 升级后没有领导者
- vba - vba 访问 Dlookup,日期为 dd/mm/yyyy 格式
- javascript - 如何将对象添加到 JavaScript 中的特定位置?
- python - 构建 Python Wheel 包时如何指定构建目录?
- java - 如何在 Java 中获取时间戳为 48962-08-06T23:16:59.000Z 的 Instant 对象
- java - 输入的所有奇数位的总和
- c - 如何检测用户何时使用程序>文件?
- python - 导入特定模块不起作用-kivy apk
- c - VS 链接器找不到 PsGetProcessWow64Process