简体   繁体   English

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

[英]Altering value of CSS property with calculated variable via JavaScript

I'm trying to set a value of a css property with a calculated variable value. 我正在尝试使用计算的变量值设置css属性的值。

I have two classes .v-table__overflow and .v-datatable__actions . 我有两个类.v-table__overflow.v-datatable__actions I want to get the width value of .v-table__overflow and set this value in .v-datatable__actions as the value of width of this class. 我想获取.v-table__overflow的宽度值,并将此值在.v-datatable__actions设置为此类的宽度值。

I'm getting this error: 我收到此错误:

Uncaught TypeError: Cannot read property 'setProperty' of undefined 未捕获的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');

Try this way 试试这个

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;

You also had a typo with using dtActionsWidth instead of dtActions in the last line which threw the error. 您在最后一行使用dtActionsWidth而不是dtActions也有错字,这会引发错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM