编辑:
您正在做的是创建一个具有值'josh'
or的 Array 文字'jessi'
,然后将其连接'_background.jpg'
起来,因此它在技术上可以工作。
问题是您缺少价值的'url()'
一部分background-image
。
'background-image': 'url(' + (bg_num == 1 ? 'josh' : 'jessi') + '_background.jpg)',
...但是您仍然应该使用()
for 分组而不是构造数组。
原答案:
使用括号代替方括号进行分组:
'background-image': (bg_num == 1 ? 'josh' : 'jessi') + '_background.jpg',
javascript 中方括号的唯一用途是获取/设置对象的属性,或创建数组文字:
var arr = []; // An Array literal
arr[10] = 'someArrValue'; // set index 10
var obj = {}; // A plain object literal
obj['prop'] = 'someObjValue'; // set the "prop" property
var key = 'prop2';
obj[key] = 'someOtherObjValue'; // set the property referenced in the "key" variable
...哦,它们当然在正则表达式语法中使用...