gpt4 book ai didi

javascript - 将 JSON 分配为属性

转载 作者:太空宇宙 更新时间:2023-11-03 21:32:09 25 4
gpt4 key购买 nike

我的类(class)有以下方法:

/**
* Loops through a JSON object and assigns the styles to the element
*/
this._assignClass = (function (className, element) {
for (var styleName in className) {
if (className.hasOwnProperty(styleName)) {

var style = element.style;

// *** THIS LINE IS NOT CORRECT ***
style.styleName = className[styleName];

}
}
});

和以下 JSON 对象:

this.configuration = {
class: {
arrowUp: {
width: "0px",
height: "0px",
borderLeft: "10px solid transparent",
borderRight: "10px solid transparent",
borderBottom: "10px solid black"
}
}
};

我这样调用方法:

this._assignClass(this.configuration.class.arrowUp, someDivElement);

在本例中,该方法应该采用 JSON 对象并将所有样式分配给对象中的元素 this.configuration.class.arrowUp。我遇到的问题是 style.styleName 被错误地解释了。如何将正确的值传递给 style

最佳答案

this._assignClass = (function (className, element) {
for (var styleName in className) {
if (className.hasOwnProperty(styleName)) {

var style = element.style;

// *** TRY THIS ONE ***
style[styleName] = className[styleName];

}
}

});

关于javascript - 将 JSON 分配为属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28688131/

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