I am trying to gather a list (array) of ids in a sector
我试图收集一个扇区中的ID列表(数组)
<div id="mydiv">
<span id='span1'>
<span id='span2'>
</div>
$("#mydiv").find("span");
gives me a jQuery object, but not a real array;
给了我一个jQuery对象,但不是一个真正的数组;
I can do
我可以
var array = jQuery.makeArray($("#mydiv").find("span"));
and then use a for loop to put the id attributes into another array
然后使用for循环将id属性放入另一个数组中
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;
无论如何,我只是想知道jQuery中是否有简写这样做;
9 个解决方案
#1
127
//but i cannot really get the id and assign it to an array that is not with in the scope?(or can I)
//但是我无法真正获取id并将其分配给不在范围内的数组?(或者我可以)
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:
请注意,当你走在正确的轨道上时,sighohwell和cletus都指出了更可靠和简洁的方法来实现这一点,利用属性过滤器(将匹配的元素限制为具有ID的元素)和jQuery的内置map()函数:
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)
#2
31
The .get() method will return an array from a jQuery object. In addition you can use .map to project to something before calling get()
.get()方法将从jQuery对象返回一个数组。此外,您可以在调用get()之前使用.map进行投影。
var idarray = $("#myDiv")
.find("span") //Find the spans
.map(function() { return this.id; }) //Project Ids
.get(); //ToArray
#3
11
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.
基本上有很多方法。
#4
7
The best way I can think of to answer this is to make a custom jquery plugin to do this:
我能想到的最好的解决方法是创建一个自定义jquery插件来执行此操作:
jQuery.fn.getIdArray = function() {
var ret = [];
$('[id]', this).each(function() {
ret.push(this.id);
});
return ret;
};
Then do something like
然后做点什么
var array = $("#mydiv").getIdArray();
#5
5
It's a late answer but now there is an easy way. Current version of jquery lets you search if attribute exists. For example
这是一个迟到的答案,但现在有一个简单的方法。当前版本的jquery允许您搜索属性是否存在。例如
$('[id]')
will give you all the elements if they have id. If you want all spans with id starting with span
you can use
如果他们有id,你会给你所有的元素。如果您希望所有范围以id开头的跨度,您可以使用
$('span[id^="span"]')
#6
3
Not a real array, but objs are all associative arrays in javascript.
不是真正的数组,但是objs都是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
我选择不使用带有[]和[] .push的真实数组,因为从技术上讲,即使这样做不正确,您也可以在页面上拥有多个ID。所以只是另一个选项,以防一些HTML重复ID
$(function() {
var oArr = {};
$("*[id]").each(function() {
var id = $(this).attr('id');
if (!oArr[id]) oArr[id] = true;
});
for (var prop in oArr)
alert(prop);
});
#7
1
You can get the ids of specifict tags and send it to a annother element. For exemple:
您可以获取特定标记的ID并将其发送到另一个元素。举个例子:
$("input").map(function() {
$( "textarea" ).append(this.id+"\n");
});
It will get all input ids and send it to textarea.
它将获取所有输入ID并将其发送到textarea。
#8
1
HTML
HTML
<div id="mydiv">
<span id='span1'>
<span id='span2'>
</div>
JQuery
JQuery的
var IDs = [];
$("#mydiv").find("span").each(function(){ IDs.push($(this).attr("id")); });
#9
-1
for(i=1;i<13;i++)
{
alert($("#tdt"+i).val());
}
#1
127
//but i cannot really get the id and assign it to an array that is not with in the scope?(or can I)
//但是我无法真正获取id并将其分配给不在范围内的数组?(或者我可以)
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:
请注意,当你走在正确的轨道上时,sighohwell和cletus都指出了更可靠和简洁的方法来实现这一点,利用属性过滤器(将匹配的元素限制为具有ID的元素)和jQuery的内置map()函数:
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)
#2
31
The .get() method will return an array from a jQuery object. In addition you can use .map to project to something before calling get()
.get()方法将从jQuery对象返回一个数组。此外,您可以在调用get()之前使用.map进行投影。
var idarray = $("#myDiv")
.find("span") //Find the spans
.map(function() { return this.id; }) //Project Ids
.get(); //ToArray
#3
11
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.
基本上有很多方法。
#4
7
The best way I can think of to answer this is to make a custom jquery plugin to do this:
我能想到的最好的解决方法是创建一个自定义jquery插件来执行此操作:
jQuery.fn.getIdArray = function() {
var ret = [];
$('[id]', this).each(function() {
ret.push(this.id);
});
return ret;
};
Then do something like
然后做点什么
var array = $("#mydiv").getIdArray();
#5
5
It's a late answer but now there is an easy way. Current version of jquery lets you search if attribute exists. For example
这是一个迟到的答案,但现在有一个简单的方法。当前版本的jquery允许您搜索属性是否存在。例如
$('[id]')
will give you all the elements if they have id. If you want all spans with id starting with span
you can use
如果他们有id,你会给你所有的元素。如果您希望所有范围以id开头的跨度,您可以使用
$('span[id^="span"]')
#6
3
Not a real array, but objs are all associative arrays in javascript.
不是真正的数组,但是objs都是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
我选择不使用带有[]和[] .push的真实数组,因为从技术上讲,即使这样做不正确,您也可以在页面上拥有多个ID。所以只是另一个选项,以防一些HTML重复ID
$(function() {
var oArr = {};
$("*[id]").each(function() {
var id = $(this).attr('id');
if (!oArr[id]) oArr[id] = true;
});
for (var prop in oArr)
alert(prop);
});
#7
1
You can get the ids of specifict tags and send it to a annother element. For exemple:
您可以获取特定标记的ID并将其发送到另一个元素。举个例子:
$("input").map(function() {
$( "textarea" ).append(this.id+"\n");
});
It will get all input ids and send it to textarea.
它将获取所有输入ID并将其发送到textarea。
#8
1
HTML
HTML
<div id="mydiv">
<span id='span1'>
<span id='span2'>
</div>
JQuery
JQuery的
var IDs = [];
$("#mydiv").find("span").each(function(){ IDs.push($(this).attr("id")); });
#9
-1
for(i=1;i<13;i++)
{
alert($("#tdt"+i).val());
}