为什么以下工作?
<something>.stop().animate(
{ 'top' : 10 }, 10
);
Run Code Online (Sandbox Code Playgroud)
虽然这不起作用:
var thetop = 'top';
<something>.stop().animate(
{ thetop : 10 }, 10
);
Run Code Online (Sandbox Code Playgroud)
为了使它更清晰:目前我无法将CSS属性作为变量传递给animate函数.
首先,我使用Cheerio进行DOM访问并使用Node.js进行解析.美好时光.
继承人的情况:
我有一个功能,我需要创建一个对象.该对象使用其键和值的变量,然后返回该单个对象.例:
stuff = function (thing, callback) {
var inputs = $('div.quantity > input').map(function(){
var key = this.attr('name')
, value = this.attr('value');
return { key : value }
})
callback(null, inputs);
}
Run Code Online (Sandbox Code Playgroud)
它输出这个:
[ { key: '1' }, { key: '1' } ]
Run Code Online (Sandbox Code Playgroud)
(.map()返回一个对象数组fyi)
我需要key实际上是来自的字符串this.attr('name').
什么是在Javascript中将字符串指定为键的最佳方法,考虑到我正在尝试做什么?