gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 01:36:52 24 4
gpt4 key购买 nike

我有一个对象,其属性的值会反复变化。除此之外,我还需要一个 css 属性的值来反射(reflect)对象的属性值。下面是我的 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'

关于javascript - 我可以使用对象属性值作为 css 属性值吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50630043/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com