【问题标题】:Access an object node with a variable in jQuery在 jQuery 中使用变量访问对象节点
【发布时间】:2014-08-10 11:16:54
【问题描述】:

我有一个像这样的 jQuery 对象(计数):

Object {men: 299, womens: 175, children: 173} 

我只是想检索值以在跨度中显示(数据过滤器属性与对象键相同)

$( "a.type_select" ).each(function() {
    var theType = $(this).attr('data-filter');
    var theCounter = counts.theType;
    $(this).children('span.count').html(' ('+theCounter+')');
});

我该怎么做?

谢谢!

【问题讨论】:

    标签: jquery variables object


    【解决方案1】:

    counts.theType 访问对象theType 的键counts

    知道对象键也可以通过字符串访问是很重要的。你只需要使用括号。例如:

    counts['men']; // 299
    

    话虽如此,您可以将变量传递给括号:

    counts[theType]
    

    这将返回你想要的。

    【讨论】:

      猜你喜欢
      • 2014-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-22
      • 1970-01-01
      • 1970-01-01
      • 2016-07-28
      相关资源
      最近更新 更多