使用Javascript设置高度
问题描述:
此代码应设置元素高度;但是没有样式被添加。我错过了明显的东西吗?使用Javascript设置高度
function setGround() {
document.getElementById('content').style.height = '40px';
}
document.onload = setGround;
的HTML是很基本的:
<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Template</title>
<link rel="stylesheet" type="text/css" href="css/default.css"/>
<link rel="stylesheet" type="text/css" href="css/orange.css"/>
<script type="text/javascript" src="javascript/detect-css.js"></script>
</head>
<body>
<header>
<div id="logo"></div>
</header>
<section id="sidebar"><p>sf </p>
</section>
<section id="content"><p>sf </p>
</section>
</body>
</html>
谢谢您的帮助!
答
请勿使用document.onload
改为使用window.onload
。
答
您可以使用此:
function setGround() {
document.getElementById('content').style.height = '40px';
}
document.onload = setGround;
但如果你想看到的变化,你应该利用这个创建于部分标记border:
<section id="content" style='border:1px solid fuchsia;' >
<p>sf </p>
</section>
答
你去哪里放置JavaScript代码?它在detect-css.js中吗?
这工作:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Template</title>
<link rel="stylesheet" type="text/css" href="css/default.css"/>
<link rel="stylesheet" type="text/css" href="css/orange.css"/>
<script language="javascript">
function resize(){
document.getElementById("content").style.height='100px';
}
</script>
</head>
<body onload="resize()">
<header><div id="logo"></div></header>
<section id="sidebar"><p>sf </p>
</section>
<section id="content" style="background-color:#CCC; display:block;"><p>sf </p>
</section>
</body>
</html>
答
,您应该使用的
window.onLoad = setGround;
代替
document.onload = setGround;
一个非常简单的解决办法是把你的脚本仅下跌了'