js给div宽度赋值
给位大哥我这么写为什么不能给div宽度赋值啊<scripttype="text/javascript">window.onload=function(){varscree...
给位大哥 我这么写为什么不能给div宽度赋值啊
<script type="text/javascript">
window.onload=function(){
var screenWidth=screen.width;
var screenHeight=screen.height;
document.getElementById('middoles').style.width=screenWidth.toString();
document.getElementById('box').style.width=screenWidth.toString();
document.getElementById('top').style.width=screenWidth.toString();
var leftWidth=(screenWidth)/2-295;
document.getElementById('contentleft').style.width=leftWidth;
document.getElementById('contentright').style.width=screenWidth/2;
}
</script> 展开
<script type="text/javascript">
window.onload=function(){
var screenWidth=screen.width;
var screenHeight=screen.height;
document.getElementById('middoles').style.width=screenWidth.toString();
document.getElementById('box').style.width=screenWidth.toString();
document.getElementById('top').style.width=screenWidth.toString();
var leftWidth=(screenWidth)/2-295;
document.getElementById('contentleft').style.width=leftWidth;
document.getElementById('contentright').style.width=screenWidth/2;
}
</script> 展开
4个回答
展开全部
楼主的代码可以用,我在 IE6、7、8 和 firefox12 里试了,都可以,可能是html部分的问题。
<style type="text/css">
div{ width:100px; height:100px; border:1px solid red;}
</style>
<script type="text/javascript">
window.onload=function(){
var screenWidth=screen.width;
var screenHeight=screen.height;
document.getElementById('middoles').style.width=screenWidth.toString();
document.getElementById('box').style.width=screenWidth.toString();
document.getElementById('top').style.width=screenWidth.toString();
var leftWidth=(screenWidth)/2-295;
document.getElementById('contentleft').style.width=leftWidth;
document.getElementById('contentright').style.width=screenWidth/2;
}
</script>
<body>
<div id="middoles">middoles</div>
<div id="box">box</div>
<div id="top">top</div>
<div id="contentleft">contentleft</div>
<div id="contentright">contentright</div>
</body>
<style type="text/css">
div{ width:100px; height:100px; border:1px solid red;}
</style>
<script type="text/javascript">
window.onload=function(){
var screenWidth=screen.width;
var screenHeight=screen.height;
document.getElementById('middoles').style.width=screenWidth.toString();
document.getElementById('box').style.width=screenWidth.toString();
document.getElementById('top').style.width=screenWidth.toString();
var leftWidth=(screenWidth)/2-295;
document.getElementById('contentleft').style.width=leftWidth;
document.getElementById('contentright').style.width=screenWidth/2;
}
</script>
<body>
<div id="middoles">middoles</div>
<div id="box">box</div>
<div id="top">top</div>
<div id="contentleft">contentleft</div>
<div id="contentright">contentright</div>
</body>
展开全部
楼主的代码可以用,我在 IE6、7、8 和 firefox12 里试了,都可以,可能是html部分的问题。
<style type="text/css">
div{ width:100px; height:100px; border:1px solid red;}
</style>
<script type="text/javascript">
window.onload=function(){
var screenWidth=screen.width;
var screenHeight=screen.height;
document.getElementById('middoles').style.width=screenWidth.toString();
document.getElementById('box').style.width=screenWidth.toString();
document.getElementById('top').style.width=screenWidth.toString();
var leftWidth=(screenWidth)/2-295;
document.getElementById('contentleft').style.width=leftWidth;
document.getElementById('contentright').style.width=screenWidth/2;
}
</script>
<body>
<div id="middoles">middoles</div>
<div id="box">box</div>
<div id="top">top</div>
<div id="contentleft">contentleft</div>
<div id="contentright">contentright</div>
</body>
<style type="text/css">
div{ width:100px; height:100px; border:1px solid red;}
</style>
<script type="text/javascript">
window.onload=function(){
var screenWidth=screen.width;
var screenHeight=screen.height;
document.getElementById('middoles').style.width=screenWidth.toString();
document.getElementById('box').style.width=screenWidth.toString();
document.getElementById('top').style.width=screenWidth.toString();
var leftWidth=(screenWidth)/2-295;
document.getElementById('contentleft').style.width=leftWidth;
document.getElementById('contentright').style.width=screenWidth/2;
}
</script>
<body>
<div id="middoles">middoles</div>
<div id="box">box</div>
<div id="top">top</div>
<div id="contentleft">contentleft</div>
<div id="contentright">contentright</div>
</body>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
onload过程中不能给dom对象赋值。
把以上的dom操作的代码,写在对应的dom元素的html代码之后。
把以上的dom操作的代码,写在对应的dom元素的html代码之后。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
后面都不要加tostring()
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询