Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 我可以使用对象属性值作为css属性值吗?_Javascript_Css_Dom - Fatal编程技术网

Javascript 我可以使用对象属性值作为css属性值吗?

Javascript 我可以使用对象属性值作为css属性值吗?,javascript,css,dom,Javascript,Css,Dom,我有一个对象,其属性的值将反复更改。 除此之外,我还需要css属性的值来镜像objects属性的值。 下面是我的javascript函数,它将与其他东西一起执行此函数 const initStats = () => { stats(); let uD = document.querySelector("#defense") let uSp= document.querySelector("#speed") let uSt = document.querySelector("#

我有一个对象,其属性的值将反复更改。 除此之外,我还需要css属性的值来镜像objects属性的值。 下面是我的javascript函数,它将与其他东西一起执行此函数

const initStats = () => {
stats();



let uD = document.querySelector("#defense")
  let uSp= document.querySelector("#speed")
  let uSt = document.querySelector("#strength")
  let uHth = document.querySelector("#health")

  uD.textContent = "Defense: " + fighterUser.defense
  let width = fighterUser.defense
  const content = document.createElement('div');
  content.classList.add('content');
  content.style.width = fighterUser.defense
  uD.appendChild(content);


  uSp.textContent = "speed: " + fighterUser.speed
  uSt.textContent = "Strength: " + fighterUser.strength
  uHth.textContent = "health: " + fighterUser.health


  let cD = document.querySelector("#defense-cpu")
  let cSp= document.querySelector("#speed-cpu")
  let cSt = document.querySelector("#strength-cpu")
  let cHth = document.querySelector("#health-cpu")

  cD.textContent = "Defense: " + fighterCpu.defense
  cSp.textContent = "Speed: " + fighterCpu.speed
  cSt.textContent = "Strength: " + fighterCpu.strength
  cHth.textContent = "health: " + fighterCpu.health
}
这是我需要帮助的部分,我正在尝试这样做,但我知道css不会把“fighterUser.defense”作为一个值

uD.textContent = "Defense: " + fighterUser.defense
  let width = fighterUser.defense
  const content = document.createElement('div');
  content.classList.add('content');
  content.style.width = fighterUser.defense
  uD.appendChild(content);

谢谢大家

您可以相对于父节点添加“px”或“%”,并指示可以测量该值:

content.style.width = fighterUser.defense + 'px'

为什么您不想使用存储简单数字的变量
var hp=38
或一个obj.property:
var bezerker={hp:-10}
原因是我正在试验并希望在每个统计下创建一个水平条形图。百分比会发生多次变化,希望我能自己解释一下?A表示已知的数量,比如汽车的最高速度130。那么,仪表标签在一定范围内工作@zer00neClick链接并阅读文章,我认为你应该在文章末尾找到答案。