如何使用JavaScript将网页高度增加X像素 [英] How to increase the page height by X pixels by using only javascript

查看:156
本文介绍了如何使用JavaScript将网页高度增加X像素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设有一个HTML网页:

Assume an HTML page:

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

进一步假设,我们能够控制的HTML页面的唯一部分是一个javascript块

Assume further that the only part of the HTML page that we're able to control is a javascript block in the middle of the page.

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

Using that javascript block, how do I increase the total page height by 150 pixels by "padding" the bottom of the page?

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

If I could alter the HTML a simple solution would be:

<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块才能实现呢?

But how do I achieve this only by using the javascript block?

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

The ideal solution should be cross-browser compatible and 100 % inline (that is using no javascript library).

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

Update #1: The javascript block has access to the entire DOM of the page.

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

Update #2: The total length of the page (before altering the DOM) varies.

更新3:假设没有javascript库。

Update #3: Assume that no javascript library (such as the otherwise excellent jQuery) can be used.

推荐答案

更新代码:

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

这篇关于如何使用JavaScript将网页高度增加X像素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆