我正在尝试使用原始 HTML/javascript 制作一个小网页。这是我的 HTML 文件的全部当前内容:
<!DOCTYPE html>
<html>
<head>
<title>My website</title>
<style>
* {
margin: 0px;
padding: 0px;
}
div.banner-light {
background-color:#9090d0;
position: absolute;
left:0px;
}
div.banner-dark {
background-color:#8080e0;
position: absolute;
left:0px;
}
</style>
</head>
<body onload="javascript:onLoadBody()">
<script>
function onLoadBody() {
var curHeight = 0;
for (var i = 1; i < 25; i++) {
document.getElementById("stat" + i.toString()).style.top = curHeight.toString() + "px";
document.getElementById("stat" + i.toString()).style.width = (screen.availWidth * 0.15).toString() + "px";
document.getElementById("stat" + i.toString()).style.height = ((window.innerHeight - curHeight) / (25 - i)).toString() + "px";
curHeight += ((window.innerHeight - curHeight) / (25 - i));
}
}
function clickDiv(id) {
document.getElementById("stat" + id).innerHTML = "Clicked";
}
</script>
<div id='stat1' class="banner-light" onclick="javascript:clickDiv(1)"><center>1</center></div>
<div id='stat2' class="banner-dark" onclick="javascript:clickDiv(2)"><center>2</center></div>
<div id='stat3' class="banner-light" onclick="javascript:clickDiv(3)"><center>3</center></div>
<div id='stat4' class="banner-dark" onclick="javascript:clickDiv(4)"><center>4</center></div>
<div id='stat5' class="banner-light" onclick="javascript:clickDiv(5)"><center>5</center></div>
<div id='stat6' class="banner-dark" onclick="javascript:clickDiv(6)"><center>6</center></div>
<div id='stat7' class="banner-light" onclick="javascript:clickDiv(7)"><center>7</center></div>
<div id='stat8' class="banner-dark" onclick="javascript:clickDiv(8)"><center>8</center></div>
<div id='stat9' class="banner-light" onclick="javascript:clickDiv(9)"><center>9</center></div>
<div id='stat10' class="banner-dark" onclick="javascript:clickDiv(10)"><center>10</center></div>
<div id='stat11' class="banner-light" onclick="javascript:clickDiv(11)"><center>11</center></div>
<div id='stat12' class="banner-dark" onclick="javascript:clickDiv(12)"><center>12</center></div>
<div id='stat13' class="banner-light" onclick="javascript:clickDiv(13)"><center>13</center></div>
<div id='stat14' class="banner-dark" onclick="javascript:clickDiv(14)"><center>14</center></div>
<div id='stat15' class="banner-light" onclick="javascript:clickDiv(15)"><center>15</center></div>
<div id='stat16' class="banner-dark" onclick="javascript:clickDiv(16)"><center>16</center></div>
<div id='stat17' class="banner-light" onclick="javascript:clickDiv(17)"><center>17</center></div>
<div id='stat18' class="banner-dark" onclick="javascript:clickDiv(18)"><center>18</center></div>
<div id='stat19' class="banner-light" onclick="javascript:clickDiv(19)"><center>19</center></div>
<div id='stat20' class="banner-dark" onclick="javascript:clickDiv(20)"><center>20</center></div>
<div id='stat21' class="banner-light" onclick="javascript:clickDiv(21)"><center>21</center></div>
<div id='stat22' class="banner-dark" onclick="javascript:clickDiv(22)"><center>22</center></div>
<div id='stat23' class="banner-light" onclick="javascript:clickDiv(23)"><center>23</center></div>
<div id='stat24' class="banner-dark" onclick="javascript:clickDiv(24)"><center>24</center></div>
</body>
</html>
奇怪的是,当我尝试在浏览器中查看这个文件时,它有时会完全按照我的意愿加载 - 页面左侧的数字 1 到 24 具有交替背景。但是,在 5 次中大约有 4 次,所有数字都会聚集在左上角,这表明错误(自己尝试一下)。
奇怪的是,你可以打开这个精确的文件 10 次而不用编辑它,而且它只会在几个实例中正常工作。我什至不认为这是可能的,但这肯定与我作为程序员所知道的一切背道而驰。如果您知道任何可能导致此问题的原因,请告诉我。顺便说一句,请原谅我可能做得不好的任何事情,因为我还很新并且自学了这一点。
编辑:在弄乱了一些关于我的 HTML、JS 和 CSS 顺序的建议解决方案之后,我意识到上面的代码是多么令人费解:我没有使用 onload,而是将 div 的样式宽度和高度设置为百分比。总之谢谢大家!