[英]dynamically store li id in the array
in my codes lis are generate dynamically and each li has special id. 在我的代码中,lis是动态生成的,每个li具有特殊的ID。 I want to store each li
"id" in the one array 我想将每个li
“ id”存储在一个数组中
this is js codes 这是js代码
var i=0;
$("ul#portfolio li").each(function(eval){
var idd = new Array();
idd[i]=$(this).attr("id");
i++;
});
but it dosn't work. 但这不起作用。
html html
<div id="container">
<ul id="portfolio" class="clearfix">
<!-- Dynamically generated li -->
</ul>
</div>
you need to use .map() 您需要使用.map()
var idd = $("#portfolio li").map(function(eval){
return this.id;
}).get();
In your case your array is local to the callback, so every iteration of the each callback you are creating a new array instead of adding the item to an existing array 在您的情况下,您的数组位于回调本地,因此每个回调的每次迭代都在创建一个新数组,而不是将项目添加到现有数组中
var idd = new Array();
$("#portfolio li").each(function (eval) {
idd.push(this.id)
});
this.id
which is the same as $(this).attr('id')
使用与$(this).attr('id')
相同的this.id
You Can try This Also 您也可以尝试
$(document).ready(function()
for(var i=0;i<5;i++){
$('#portfolio').append("<li id='"+ i +"'>"+ i +"</li>");
}
var idArray=new Array();
$('#portfolio li').each(function(ind,val){
idArray.push(this.id)
});
console.log(idArray);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.