javascript实现并列的DIV高度自适应 10
functionsetHeight(){varobjContent=document.getElementById('content');varobjSide=docum...
function setHeight() {
var objContent = document.getElementById('content');
var objSide = document.getElementById('side');
if (objContent.style.height > objSide.style.height) {
objSide.style.height = objContent.style.height;
}
else {
objContent.style.height = objSide.style.height;
}
}
Content和side是两个浮动的并列的DIV,目的是实现Content和Side高度自适应,
然后在<body onload="setHeight">
这段代码并没有实现,错在哪里?
sorry,我写问题写错了,代码没错..
是这样<body onload="setHeight()"> 展开
var objContent = document.getElementById('content');
var objSide = document.getElementById('side');
if (objContent.style.height > objSide.style.height) {
objSide.style.height = objContent.style.height;
}
else {
objContent.style.height = objSide.style.height;
}
}
Content和side是两个浮动的并列的DIV,目的是实现Content和Side高度自适应,
然后在<body onload="setHeight">
这段代码并没有实现,错在哪里?
sorry,我写问题写错了,代码没错..
是这样<body onload="setHeight()"> 展开
2个回答
展开全部
javascript应该有问题,你应该这样写:
function setHeight() {
var objContent = document.getElementById('content');
var objSide = document.getElementById('side');
if (objContent.offsetHeight> objSide.offsetHeight) {
objSide.style.height = objContent.offsetHeight + "px";
}
else {
objContent.style.height = objSide.offsetHeight + "px";
}
}
还有就是这段javascript最好放在这两个div的下面,如果你当独写成一个.js文件的话,就在<head></head>这里引用,然后在这两个div下面写这一段:
<script language="javascript">
setHeight() ;
</script>
然后body那里就不要写onload="setHeight() " 了
function setHeight() {
var objContent = document.getElementById('content');
var objSide = document.getElementById('side');
if (objContent.offsetHeight> objSide.offsetHeight) {
objSide.style.height = objContent.offsetHeight + "px";
}
else {
objContent.style.height = objSide.offsetHeight + "px";
}
}
还有就是这段javascript最好放在这两个div的下面,如果你当独写成一个.js文件的话,就在<head></head>这里引用,然后在这两个div下面写这一段:
<script language="javascript">
setHeight() ;
</script>
然后body那里就不要写onload="setHeight() " 了
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询