Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/414.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 将JSON指定为属性_Javascript_Css_Json - Fatal编程技术网

Javascript 将JSON指定为属性

Javascript 将JSON指定为属性,javascript,css,json,Javascript,Css,Json,我在班上有以下方法: /** * 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 st

我在班上有以下方法:

/**
 * 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];

        }
    }
}))


}))

样式只是一个对象,因此可以使用
[]
指定其属性

this._assignClass = (function (className, element) {
    for (var styleName in className) {
        if (className.hasOwnProperty(styleName)) {
            //simply this would work
            element.style[styleName] = className[styleName];
        }
    }
});

样式只是一个对象,因此可以使用
[]
指定其属性

this._assignClass = (function (className, element) {
    for (var styleName in className) {
        if (className.hasOwnProperty(styleName)) {
            //simply this would work
            element.style[styleName] = className[styleName];
        }
    }
});

高度:“0px”,
高度:“0px”
-缺少一个comma@jdphenix,我修正了那个错误。上述问题仍然存在。当-你总是希望它是简单的:)
高度:“0px”,而不是
高度:“0px”
-缺少一个comma@jdphenix,我修正了那个错误。上述问题仍然存在。当-你总是希望它是简单的:)