查看以下假设代码:
// create some list we work with
$('body').html('<ul class="collection-list"></ul>');
// some array with string data
var collection = ['foo', 'bar', 'foobar'];
// here we store our jquery objects
var doms = [];
// for each item in the collection array we create a li
$.each(collection, function(index, value) {
// imagine that the jquery part would return a reference/object which we push into our doms array
doms.push($('ul.collection-list').append('<li class="item">' + value + '</li>'));
});
// we could now do different logic through our cached object
doms[2].val('new value');
doms[1].remove();
可能是该示例没有任何逻辑意义,但请不要向我展示替代方案!我只想使用提出的技术。例子只是一个例子!