JS控制DIV高度,同理可实现控制DIV宽度变化。具体方法如下:
方法一:
<div id="div" style="width: 100px; background: #F90;">test</div>
<script type="text/javascript">
var div = document.getElementById("div");
var width=parseInt(div.style.width) || div.offsetWidth;
div.style.height=width*2 + "px";
</script>
方法二:
<div id="div" style="width: 100px; background: #F90;">test</div>
<script type="text/javascript">
var div = document.getElementById("div");
var width=div.style.width.substr(0,div.style.width.length-2);
div.style.height=width*2 + "px";
</script>