简体   繁体   中英

Set object property from variable

This is the example:

var side = 'left';

$('div').css({
  position: 'relative',
  side: 50
});

I put variable side instead real property name. I was expecting that this will work, but... :)

So, how to set property name from varaibles value?

The only way is to declare previously your object as a variable...

var options = {
  position: 'relative'
};
options[side] = 50;
$('div').css(options);

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