可以在Javascript中选择属性“高度”吗?

问题描述

我只想在CSS设置的图像中使用高度,然后在JS中捕获此渐变并将宽度缩放到高度x2.25。

有可能吗?

解决方法

您可能想设置样式或set attribute

在此示例中,我直接设置样式

const imgEl = document.querySelector('img')
console.log('imgEl height:',imgEl.getAttribute('height'))

const divEl = document.querySelector('div#d1')
divEl.style.width = String(+imgEl.getAttribute('height') * 2.25) + 'px'
#d1,#d2 {
  border-style: solid;
  margin: 10px;
  height: 100px;
  width: 100px;
}
<img height="150" width="150" src="https://picsum.photos/150/150" />
<div id="d1">div1</div>
<br/>
<div id="d2">div2</div>