简体   繁体   English

JavaScript样式元素

[英]Javascript styling element

I'm trying to make a function that will set css params to element. 我正在尝试制作一个将css params设置为element的函数。 I have the following code: 我有以下代码:

var css = function(k, l){
    for(key in l){
        k.style.key = l[key];
    }
}

But it is not working. 但这是行不通的。 I think it's because of the key variable. 我认为是因为有关键变量。 Is there some way to make it work? 有什么办法可以使它工作?

First, declare key as var in order to make it strict, and then, you have to access the property of style with brackets: 首先,将key声明为var以使其严格,然后,必须使用方括号访问style的属性:

var css = function (k, l) {
    for (var key in l) {
        k.style[key] = l[key];
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM