如何用js 获取一个DIV宽 并赋值给另一个DIV的宽的代码怎么写请高手指点指点
js 获取DIV宽 并赋值给另一个DIV
- 提问者网友:兔牙战士
- 2021-08-10 10:31
- 五星知识达人网友:动情书生
- 2021-08-10 10:52
<!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>
- 1楼网友:掌灯师
- 2021-08-10 12:11
你试试这样吧
<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>