![](/img/trans.png)
[英]How can I set the height of element to the actual required height of an HTML element, as opposed to auto in CSS or JavaScript?
[英]How I can get css property and change the height of html element by javascript
我尝试创建一个 div 元素并从 CSS 选择器读取高度。 最后通过 javascript 更改 div 元素高度。 当我使用div.style.height ='15px';
工作正常,但我想动态改变高度,因为我的 CSS class 将通过响应 function 改变。
我的 CSS 是:
#indicator {
width: 15px;
height: 15px;
}
我将此代码用于脚本:
div = document.createElement("div");// valve_indicator
div.style.border = '1px solid #b3280a';
div.style.height =document.getElementById('indicator').style.height; // instead div.style.height ='15px';
div.style.width = '15px';
cell.appendChild(div); // cell is a table cell
但是document.getElementById('indicator').style.height
返回 null。 请帮助我提前谢谢
删除#indicator
中的#
。 getElementById()
在 id 名称前不需要#
符号
document.getElementById('indicator').style.height
如果您想动态更改indicator
height
:
let indicator = document.getElementById('indicator');
indicator.addEventListener("change", function(){
div.style.height = indicator.style.height;
})
我通过使用此代码解决了我的问题
div.className='indicator';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.