我如何获得 css 属性 并通过 javascript 更改 html 元素的高度

How I can get css property and change the height of html element by javascript

我尝试创建一个 div 元素并从 CSS 选择器读取高度。最后将 div 元素高度更改为 javascript。当我使用 div.style.height ='15px'; 时工作正常,但我想动态更改高度,因为我的 CSS class 将通过响应功能更改。

我的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.heightreturn无效。 请帮我提前谢谢

删除 #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';