gpt4 book ai didi

eval - 如何在 JavaScript 命令中使用变量?

转载 作者:行者123 更新时间:2023-11-28 16:17:10 25 4
gpt4 key购买 nike

将一个简单的 JS 函数视为

function test(property, color) {
var element = document.getElementById("test");
element.style.PROPERTY = color; // I want to use a variable for this command
}

element.style.PROPERTY的典型JS命令中,我想将属性作为变量引入。这是一个非常基本的例子;在 JS 命令行中使用变量的最佳方法是什么?

最佳答案

将其用作:

element.style[property]

This is a very basic example; what is the best approach to use variables in JS command lines?

这取决于您想要实现的目标。在这种情况下 - 只需使用 [...]

关于eval - 如何在 JavaScript 命令中使用变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11131053/

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