简体   繁体   中英

Setting style attributes (width, etc) through a function argument in javascript?

What I'd like to do is create a function that I can call whenever I need to set a style on an object. I want it to look like this:

this.setCSS = function(attribute, value) {
  myHTMLNode.style.attribute = value;
}

The arguments being passed in right now are strings, so I thought eval() might do it, but I would like to avoid that because it isn't very efficient.

Any ideas at all?

采用

myHTMLNode.style[attribute] = value;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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