开发者

jquery: most efficient way to get and store the ids of 100+ td items?

Ok so I have about six of these calendars on page, so thats around 180 td items (1 for each date):

jquery: most efficient way to get and store the ids of 100+ td items?

each td has an id that is equal to that day's timestamp. the dates I开发者_运维百科 want are the red ones (class of .booked).

So i need the fastest way to 'serialize' the ids for td.booked items, any ideas?


You can get an array of the IDs using .map(), like this:

var ids = $("td.booked").map(function() { return this.id; }).get();

This would result in an array like this:

["id1", "id2", "id3", ... ]


What exactly do you mean by "serialize"?

If they are being marked on the client, then the fastest way to collect references to them would be to collect them as they are marked. Then, you already an index to them when you need it, and no retroactive query is needed.


How about this:

var str = '';

$(function(){
  $('td.booked').each(function(index){
     str += '&td' + index + "=" + encodeURIComponent($(this).text());
  });
});

alert(str);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜