Javascript 什么会导致函数的最后两行不执行?
这段js代码的最后几行不会执行,除非我将它们放在另一个函数中,并使用html输入执行它们Javascript 什么会导致函数的最后两行不执行?,javascript,html,function,Javascript,Html,Function,这段js代码的最后几行不会执行,除非我将它们放在另一个函数中,并使用html输入执行它们 function baseSize() { var available = window.screen.availHeight; var windowHeight = window.screen.height; var makeHeight = 849 - windowHeight; document.body.style.backgroundPosition = "0px
function baseSize() {
var available = window.screen.availHeight;
var windowHeight = window.screen.height;
var makeHeight = 849 - windowHeight;
document.body.style.backgroundPosition = "0px -" + makeHeight + "px";
var container = document.getElementById("container");
container.style.height = available + "px";
var right = document.getElementById("right");
right.style.height = available - 230 + "px";
var left = document.getElementById("left");
left.style.height = available - 230 + "px";
var storyright = document.getElementById("storyright");
storyright.style.height = available - 40 + "px";
}
这是为什么?我该如何解决它?(我知道它可能很凌乱,所以不要建议收紧它的方法。我稍后再讨论。)
这是html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<link rel="stylesheet" type="text/css" href="stories.css" />
<script type = "text/javascript" src="script.js">
</script>
<div id = "container">
<body onLoad = "baseSize(); storySize();">
<div id = "head">
<table class="links">
<tr>
<td class="link"><a href="index.html">Home</a></td>
<td class="link"><a href="poetry.html">Poetry</a></td>
<td class="link"><a href="essays.html">Essays</a></td>
<td class="link"><a href="stories.html">Stories</a></td>
<td class="about"><a href="about.html">About</a></td>
</tr>
</table>
<div id = "icon"><p>Craig InTheDell</p></div>
</div>
<div id="container2">
<div id="top">
<table>
<tr>
<td class="button"><input type="image" src="mostrecentbutton.jpg" onClick="showContent1()"/></td>
<td class="button"></td>
<td class="button"></td>
<td></td>
</tr>
</table>
</div>
<div id="storyright">
<div id="content1">
<input type = "button" Value = "click me!" onClick = "storySize()"/>
</div>
</div>
</div>
</body>
</div>
</html>
克雷格·因特尔
如果var left
没有style
或style.height
属性,它可能会杀死javascript。查看您的控制台-出现任何错误吗?您能给我们看一下HTML吗?很好。。。这也是一个确定的可能性。你说的未定义是什么意思?第二行到最后一行不是定义了它吗?@Matthew:如果JavaScript执行时页面上不存在带有“storyright”ID的HTML元素,那么document.getElementById
将返回undefined
,然后将其分配给storyright
变量。好的,问题是没有“left”或“right”div,所以我在中添加了这些div,并将每个div的display设置为none,这就解决了问题。我正试图将所有的js保存在一个文件中,因此存在一些重叠。@Nicholauschiping看起来你是对的,他的评论是“除了最后两行,所有东西都可以工作。”“让我相信样式规则适用于storyright元素之上的元素。