gpt4 book ai didi

javascript - 如何仅使用 javascript 将页面高度增加 X 像素

转载 作者:太空宇宙 更新时间:2023-11-04 14:25:07 25 4
gpt4 key购买 nike

假设一个 HTML 页面:

<html>
<body>
.. html content (outside of our control) ..
.. javascript block ..
.. some more html content (outside of our control) ..
</body>
</html>

进一步假设我们能够控制的 HTML 页面的唯一部分是页面中间的 javascript block 。

使用该 javascript block ,如何通过“填充”页面底部将总页面高度增加 150 像素?

如果我可以更改 HTML,一个简单的解决方案是:

<html>
<body>
.. html content (outside of our control) ..
.. javascript block ..
.. some more html content (outside of our control) ..
<div style="height: 150px; clear: both;"></div>
</body>
</html>

但是我如何仅通过使用 javascript block 来实现这一点?

理想的解决方案应该是跨浏览器兼容和 100% 内联(即不使用 javascript 库)。

更新 #1:javascript block 可以访问页面的整个 DOM。

更新#2:页面的总长度(在更改 DOM 之前)有所不同。

更新 #3: 假设没有可以使用的 javascript 库(例如优秀的 jQuery)。

最佳答案

更新代码:

var space = document.createElement('div');
space.style.height = "150px";
space.style.clear = "both";
document.getElementsByTagName("body")[0].appendChild(space);

关于javascript - 如何仅使用 javascript 将页面高度增加 X 像素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/799743/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com