mootools 美元符号元素调用中的变量
所以我有一个想要修改的元素(使用 Fx.Tween,但我认为这并不重要)。然而,元素 id 是动态生成的,这意味着我必须将它从一些变量中拼凑起来。
那么假设...(在 js 中)
name = 'foo';
id = '42';
并且我想访问元素 $('foo_42')
...我将如何输入它?
$(name+'_'+id) 似乎不起作用,除非我做错了......?
我的代码中的实际示例:
var highlight = new Fx.Tween($(accountID+'_'+type+'_'+permission), {
background-color: #f00;
});
更新:看起来这个问题没有答案 - 代码示例中的 JS 是错误的......由于 Fx.Tween 函数的错误使用。谢谢大家。
So I have an element I want to modify (with Fx.Tween, but I suppose it doesn't really matter). However, the element id is dynamically generated, meaning I have to piece it together from some variables.
So let's say... (in js)
name = 'foo';
id = '42';
and I want to access element $('foo_42')
... how would I type it out?
$(name+'_'+id) doesn't seem to work, unless I'm doing it wrong...?
Actual example from my code:
var highlight = new Fx.Tween($(accountID+'_'+type+'_'+permission), {
background-color: #f00;
});
Update: Looks like this question had no answer - my JS in the code sample is just wrong... due to incorrect usage of the Fx.Tween function. Thanks all.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,差不多就是这样。 Mootools 不会知道您是否执行
$('foo_42')
或$('foo' + '_' + '42')
,它只会看到 <代码>foo_42。只要确保该 ID 确实存在即可。如果不存在,则$()
将返回null
。No, that's pretty much exactly it. Mootools won't know if you do
$('foo_42')
or$('foo' + '_' + '42')
, all it will see isfoo_42
. Just make sure that ID actually exists. If it doesn't, then$()
will returnnull
.你尝试过吗
?您发布的原始代码不是有效的 Javascript。请注意,JS 对象语法不是 CSS。
只要在该范围内定义了
name
和id
,语法$(name+'_'+id)
就必须有效。Have you tried
? The original code you posted isn't valid Javascript. Note that the JS object syntax is not CSS.
The syntax
$(name+'_'+id)
must work as long asname
andid
are defined in that scope.