admin管理员组文章数量:1410737
I need to choose a property to animate and then, do animation.
The code should be like a following:
var prop = "background-color";
switch( val )
{
case 1: prop = "color";
case 2: prop = "border-color";
// ...
}
item.animate( {prop: "#00FF00"}, 1000 );
JavaScript plains about using "prop" variable.
When i just say
item.animate( {"color": "#00FF00"}, 1000 );
everything is fine.
I think, a constant is expected as object property declaration.
How can i determine it at runtime ?
I need to choose a property to animate and then, do animation.
The code should be like a following:
var prop = "background-color";
switch( val )
{
case 1: prop = "color";
case 2: prop = "border-color";
// ...
}
item.animate( {prop: "#00FF00"}, 1000 );
JavaScript plains about using "prop" variable.
When i just say
item.animate( {"color": "#00FF00"}, 1000 );
everything is fine.
I think, a constant is expected as object property declaration.
How can i determine it at runtime ?
Share Improve this question edited Jun 8, 2011 at 1:54 Billy ONeal 107k61 gold badges329 silver badges563 bronze badges asked Nov 2, 2010 at 9:11 AntonALAntonAL 17.4k21 gold badges84 silver badges118 bronze badges2 Answers
Reset to default 5These are equivalent:
// prop is a literal string here,
// not a variable
{prop: "#00FF00"}
and
{"prop": "#00FF00"}
you probably need to do something like this:
var obj = {};
obj[prop]="#0000ff";
item.animate( obj, 1000 );
Yes, you're correct, JavaScript expects an identifier as the property name in an object literal. You'll have to create an object and assign the property using square bracket notation.
var opts = {};
opts[prop] = "#00FF00";
item.animate(opts, 1000);
本文标签: javascriptUse string variable as object fieldsStack Overflow
版权声明:本文标题:javascript - Use string variable as object fields - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744844388a2628112.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论