anArray = ['thing1','thing2','thing3'];
$.each(anArray, function (i,el) {
var object = 'name.space.' + el;
var selector = 'node-' + el;
var object = $('#' + selector);//need object here to be interpreted as it's value
//as if: var name.space.thing1 = $('#' + selector);
});
这些是可用的 jQuery 对象:
console.log(name.space.thing1);
console.log(name.space.thing2);
console.log(name.space.thing3);
我觉得涉及到 eval()。我正在整合导航选择器,以便在添加/删除页面时,我们只更新数组。我们可以从导航节点构建数组,但无论哪种方式,我们仍然需要能够制作这些命名空间选择器...
最佳答案
您将不得不使用括号表示法:
var array = ['thing1', 'thing2'];
var object = {};
object.space = {};
$.each(array, function () {
object.space[this] = $('#node-' + this);
});
console.log(object.space.thing1); // [<div id="node-1">];
关于javascript - 如何从数组创建 jQuery 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7572996/