当前位置: 动力学知识库 > 问答 > 编程问答 >

jquery - Creating a set of elements from a set of `id`s

问题描述:

I want to create a set of elements out from an array of ids so that it can be used as a jquery receiver. For example, if I have ids: id1, id2, id3, how can I turn this into a jquery selector so that the corresponding elements appear in this order? Supposing what I want is theJquerySelector, I then would like to apply a jquery operation like this:

theJquerySelector.appendTo($(...))

where $(...) is another jquery selector irrelevant to the question.

网友答案:

You could use Arrays join() method as such;

$('#' + yourArrayOfIds.join(', #')).appendTo($(/* */));

A more jQuery-esque way would be to either write a utility function to do this for you on the jQuery object (and then call it a "plugin"), or to add classes to the relevant elements and select via that.

网友答案:

You can use ^ to select attributes that start with a string.

$("[id^=id]").appendTo($(..));

To sort them first

$($("[id^=id]").toArray().sort(function(a, b) { a.id - b.id } )).appendTo()

It seems you have to rewrap the result of sort to get a jQuery object, not sure why.

分享给朋友:
您可能感兴趣的文章:
随机阅读: