通过JavaScript使用计算的变量更改CSS属性的值

时间:2019-06-26 05:23:18

标签: javascript html css variables properties

我正在尝试使用计算出的变量值设置css属性的值。

我有两个班.v-table__overflow.v-datatable__actions。我想获取.v-table__overflow的width值,并在.v-datatable__actions中将此值设置为此类的width值。

我收到此错误:

  

未捕获的TypeError:无法读取未定义的属性'setProperty'

var dtoverflow = document.querySelector('.v-table__overflow')
const style4 =  getComputedStyle(dtoverflow);
var dtoverflowWidth = style4.width 

var dtActions = document.querySelector('.v-datatable__actions') 
const style5 =  getComputedStyle(dtActions); 
var dtActionsWidth = style5.width;

dtActionsWidth.style.setProperty('width', '--dtoverflowWidth');

1 个答案:

答案 0 :(得分:1)

尝试这种方式

var dtoverflow = document.querySelector('.v-table__overflow')
var dtoverflowWidth = getComputedStyle(dtoverflow).width;

var dtActions = document.querySelector('.v-datatable__actions') 
var dtActionsWidth = getComputedStyle(dtActions).width;

dtActions.style.width = dtoverflowWidth;

在最后一行中,您还使用dtActionsWidth而不是dtActions出现了错字。