在Jquery/Javascript中访问动态属性名称 [英] Accessing dynamic property names in Jquery/Javascript

查看:60
本文介绍了在Jquery/Javascript中访问动态属性名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我正在编写的插件中,开发人员可以指定选项,这些选项我可以像这样存储和引用:

In a plugin I'm writing, the dev can specify options, which I'm storing and referencing like so:

(function( $, window) {
    $.widget("mobile.plug", $.mobile.widget, {
        options: {
            menuWidth: '25%',
            middleWidth: '25%',
            mainWidth: '25%'
            },
         some: function(){
            var self = this,
                o = self.options;

            console.log( o.menuWidth );
            }
     })
}) (jQuery,this);

我的问题:
假设我想遍历所有三个元素(主要,菜单,中间)并获取各自的选项值,如果可能的话,我将如何动态构造 o.[elem] Width ?

My Question:
Say I want to loop through all three elements (main, menu, middle) and get the respective option value, how would I construct o.[elem]Width dynamically, if at all possible?

这不起作用(错误:.运算符后缺少名称):

This doesn't work (ERROR: missing name after . operator):

// this selects panels with jqmData(panel="mid|menu|main")
var elems = $('selector');

for (var i = 0; i<elems.length; i++){
   var el = elems.eq(i);
   console.log( o.[el.jqmData("panel")]Width );
   }

推荐答案

您应该可以将宽度"连接到面板"结果:

You should be able to concatenate "Width" to the "panel" result:

o[el.jqmData("panel") + "Width"]

例如,如果el.jqmData("panel")是菜单",您将得到o["menuWidth"].

E.g., if el.jqmData("panel") is "menu" you would get o["menuWidth"].

这篇关于在Jquery/Javascript中访问动态属性名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆