简体   繁体   中英

How to get children of parent element?

]Hi can you please tell me how to get count children of parent element ?In my demo I add submenu by pressing "add" button it generate submenu of "first level" in left panel .I need count children if user click to "first level"

In a pic if user have 3 children .If it is click "First level" it show "3",If user click "menu_tc_1" it show 0..or same in "menu_tc_2", "menu_tc_3"

http://jsfiddle.net/rajumjib/PZR7N/6/ .

$(document).on('click'," ul li > a",function(e){
 alert('----'+$(this).siblings().length);  // working fine
     alert('----'+$(this).siblings().first().attr('id')); //not working ?


})

can I gey ID of first child ..?

So, answer with both requirements:

$(document).on('click'," ul li > a",function(e){
     alert($(this).siblings().length)
     var id=$(this).parent().attr("id") || $(this).parents("ul").attr("id");
    alert(id);
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM