嗨,您能告诉我如何获取所有ID的兄弟姐妹数组。我有兄弟姐妹的长度。现在如何在数组中获取ID的ID。。
在图片中,如果用户有3个孩子。如果是按“第一级”,它显示“3”,如果用户单击menu_tc_1
显示它0
..或在同一menu_tc_2
,menu_tc_3
。如何获取数组中的同级ID。我尝试这个但没有ID
$(document).on('click'," ul li > a",function(e){
//alert($(this).siblings().length);
//first method..
console.log($(this).siblings().length);
/*var id=$(this).parent().attr("id") || $(this).parents("ul").attr("id");
console.log(id);*/
var selEl = [];
$(this).siblings().each(function (idx, el) {
selEl.push('#' + el.id);
});
console.log(selEl)
// alert(id);
// getViewFromPanel();
})
试试这个,
var arr=$(this).siblings().map(function () {
return '#' + this.id;
}).get();
console.log(arr);
根据您的情况进行了更新,没有可以siblings of a
尝试的方法,
var selEl = [];
$(this).closest('ul').find('li').each(function (idx, el) {
// -----^ using closest and find, as there are no siblings of a
selEl.push('#' + el.id);
});
console.log(selEl);
另外,如果您要siblings
使用,请在li上而不是在锚标记上使用click事件,
$(document).on('click', " ul li", function (e) {
// use click event on li not on anchor tag
//alert($(this).siblings().length);
......
var selEl = [];
$(this).siblings().each(function (idx, el) {
selEl.push('#' + el.id);
});
console.log(selEl);
.....
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句