我的课堂上有以下方法:

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

        }
    }


});

08-16 15:22