js 获取DIV宽 并赋值给另一个DIV
2013-08-15
展开全部
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns=" http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<style type="text/css">
div{height:30px;}
</style>
</head><body>
为了说明问题加上背景和边框
<div id="x" style="width:100%; border:1px solid #000000;">
<div style="width:10px;float:left;background-color:#33CC99;"></div>
<div id="y" style="width:auto; float:left;background-color:#6600CC;" >嵌套的</div>
<div style="float:left;width:20px;background-color:#FF0000;"></div>
</div>
<script>
var x = document.getElementById("x");
var y = document.getElementById("y");
x.style.width=y.offsetWidth+10+20+"px";
</script>
</body>
</html>
<html xmlns=" http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<style type="text/css">
div{height:30px;}
</style>
</head><body>
为了说明问题加上背景和边框
<div id="x" style="width:100%; border:1px solid #000000;">
<div style="width:10px;float:left;background-color:#33CC99;"></div>
<div id="y" style="width:auto; float:left;background-color:#6600CC;" >嵌套的</div>
<div style="float:left;width:20px;background-color:#FF0000;"></div>
</div>
<script>
var x = document.getElementById("x");
var y = document.getElementById("y");
x.style.width=y.offsetWidth+10+20+"px";
</script>
</body>
</html>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-08-15
展开全部
你试试这样吧 <div style="background-color:blue;width:120px;" id="x">这个120</div>
<div style="background-color:green;width:180px;" id="y">给这个也120吧</div>
<input type="button" value="给吧" onclick="xy();" />
<script>
function xy(){
var a = document.getElementById("x").style;
var b = document.getElementById("y").style;
b.width = a.width;
}
</script>
<div style="background-color:green;width:180px;" id="y">给这个也120吧</div>
<input type="button" value="给吧" onclick="xy();" />
<script>
function xy(){
var a = document.getElementById("x").style;
var b = document.getElementById("y").style;
b.width = a.width;
}
</script>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询