[英]Selecting <span />s inside a <li /> inside a <ul /> with jQuery
I have the next code: 我有下一个代码:
<ul>
<li id="cat-12">
<span id="subcat-13">lorem</span>
<span id="subcat-43">ipsum</span>
</li>
<li id="cat-41">
<span id="subcat-22">lorem</span>
<span id="subcat-23">ipsum</span>
</li>
</ul>
How I can select all <span />
IDs? 如何选择所有<span />
ID? So I get an array with: subcat-13, subcat-43, subcat-22, subcat-23? 所以我得到一个数组:subcat-13,subcat-43,subcat-22,subcat-23?
Thank you in advance! 先感谢您!
No need to loop with each
, instead use map
( docs ) like this: 不需要与each
循环,而是使用map
( docs )像这样:
var arr = $('ul li span').map(function(){
return $(this).attr('id');
});
Live example: http://jsfiddle.net/qdAnx/ 实时示例: http : //jsfiddle.net/qdAnx/
var array = new Array();
$('ul > li > span').each(function() {
array.push($(this).attr('id'));
});
if you want a pure Array with just the ID you can select all the span: 如果您只需要ID的纯数组,则可以选择所有范围:
var span = $("ul li span");
and then loop them with .each() 然后用.each()循环它们
var span = $("ul li span"),
arr = [];
span.each(function() {
if(this.id) {
arr.push(this.id);
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.