admin管理员组文章数量:1134600
I am trying to gather a list (array) of ids in a sector
<div id="mydiv">
<span id='span1'>
<span id='span2'>
</div>
$("#mydiv").find("span");
gives me a jQuery object, but not a real array;
I can do
var array = jQuery.makeArray($("#mydiv").find("span"));
and then use a for loop to put the id attributes into another array
or I can do
$("#mydiv").find("span").each(function(){}); //but i cannot really get the id and assign it to an array that is not with in the scope?(or can I)
Anyhow, I just wanna see if there is a shorthand in jQuery to do that;
I am trying to gather a list (array) of ids in a sector
<div id="mydiv">
<span id='span1'>
<span id='span2'>
</div>
$("#mydiv").find("span");
gives me a jQuery object, but not a real array;
I can do
var array = jQuery.makeArray($("#mydiv").find("span"));
and then use a for loop to put the id attributes into another array
or I can do
$("#mydiv").find("span").each(function(){}); //but i cannot really get the id and assign it to an array that is not with in the scope?(or can I)
Anyhow, I just wanna see if there is a shorthand in jQuery to do that;
Share Improve this question edited Jan 10, 2016 at 19:21 Bhargav Rao 52k29 gold badges126 silver badges141 bronze badges asked May 5, 2009 at 22:55 Roy ChanRoy Chan 2,9186 gold badges36 silver badges43 bronze badges 09 Answers
Reset to default 167//but i cannot really get the id and assign it to an array that is not with in the scope?(or can I)
Yes, you can!
var IDs = [];
$("#mydiv").find("span").each(function(){ IDs.push(this.id); });
This is the beauty of closures.
Note that while you were on the right track, sighohwell and cletus both point out more reliable and concise ways of accomplishing this, taking advantage of attribute filters (to limit matched elements to those with IDs) and jQuery's built-in map()
function:
var IDs = $("#mydiv span[id]") // find spans with ID attribute
.map(function() { return this.id; }) // convert to set of IDs
.get(); // convert to instance of Array (optional)
The .get() method will return an array from a jQuery object. In addition you can use .map to project to something before calling get()
var idarray = $("#myDiv")
.find("span") //Find the spans
.map(function() { return this.id; }) //Project Ids
.get(); //ToArray
My suggestion?
var arr = $.map($("#mydiv [id]"), function(n, i) {
return n.id;
});
you could also do this as:
var arr = $.map($("#mydiv span"), function(n, i) {
or
var arr = $.map($("#mydiv span[id]"), function(n, i) {
or even just:
var arr = $("#mydiv [id]").map(function() {
return this.id;
});
Lots of ways basically.
The best way I can think of to answer this is to make a custom jquery plugin to do this:
jQuery.fn.getIdArray = function() {
var ret = [];
$('[id]', this).each(function() {
ret.push(this.id);
});
return ret;
};
Then do something like
var array = $("#mydiv").getIdArray();
It's a late answer but now there is an easy way. Current version of jquery lets you search if attribute exists. For example
$('[id]')
will give you all the elements if they have id. If you want all spans with id starting with span
you can use
$('span[id^="span"]')
Not a real array, but objs are all associative arrays in javascript.
I chose not to use a real array with [] and [].push because technically, you can have multiple ID's on a page even though that is incorrect to do so. So just another option in case some html has duplicated ID's
$(function() {
var oArr = {};
$("*[id]").each(function() {
var id = $(this).attr('id');
if (!oArr[id]) oArr[id] = true;
});
for (var prop in oArr)
alert(prop);
});
HTML
<div id="mydiv">
<span id='span1'>
<span id='span2'>
</div>
JQuery
var IDs = [];
$("#mydiv").find("span").each(function(){ IDs.push($(this).attr("id")); });
You can get the ids of specifict tags and send it to a annother element. For exemple:
$("input").map(function() {
$( "textarea" ).append(this.id+"\n");
});
It will get all input ids and send it to textarea.
for(i=1;i<13;i++)
{
alert($("#tdt"+i).val());
}
本文标签: javascriptHow to get all of the IDs with jQueryStack Overflow
版权声明:本文标题:javascript - How to get all of the IDs with jQuery? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736831087a1954699.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论