获取使用每个循环时每个父节点的 ID
Get the ID of the each parent node while using Each Loop
我可以获取每个循环中父节点的ID。但唯一的问题是在我的解决方案中,我在控制台中得到这样的:
test-1 // test-1 id
Test1
test-1 // test-1 id
Test2
test-1 // test-1 id
Test3
test-2 // test-2 id
Test1
test-2 // test-1 id
Test2
test-3
Test1
我期望的解决方案是:
test-1 // test-1 id
Test1
Test2
Test3
test-2 // test-2 id
Test1
Test2
test-3 // test-3 id
Test1
检查我的小提琴:https://jsfiddle.net/5muotp9n/1/
试着这样重写你的逻辑,
$('#test .demo').each(function () {
console.log(this.id);
$("ul li", this).each(function(){
console.log(" "+this.textContent)
});
});
演示
你要做的是在循环中做一个循环,所以像这样:
$('#test ul').each(function () {
var parents = $(this).parents('.demo').attr('id');
console.log("Parent : " + parents);
$(this).find('li').each(function(){ //for each li
console.log($(this).html());
});
});
这应该可以解决问题:)
$('.demo').each(function () {
var parents = $(this).attr('id');
console.log(parents);
$(this).find("li").each(function(){
console.log(" "+$(this).text());
});
});
https://jsfiddle.net/5muotp9n/1/
相关文章:
- 在自定义截面树中获取父节点
- 如何避免在取消选中子节点时取消选中父节点
- 如果未选中子节点,则未选中父节点
- 仅使用某些子节点访问xml中父节点的子节点
- 如何以编程方式在 jsTree 中选择一个节点并打开所有父节点
- 使用Javascript设置带有子节点的父节点的类's文本等于.
- 如何确定父节点是否有子节点
- Javascript在没有jQuery的情况下获取X父节点
- 如何从wijmo-flex网格的树视图中的节点转到父节点
- 在没有 jquery 的情况下更改子节点(父节点具有 ID)中的链接文本
- JQuery 选择父节点并更改其类名
- 剑道ui树视图dragend获取节点id
- 如何删除javascript中的节点,而不需要父节点为null
- 如果未全部选中子节点,则取消选中父节点
- 如何在JavaScript中删除子节点触发点击事件时的父节点
- 获取使用每个循环时每个父节点的 ID
- 无法获取父节点id
- 从多个树视图中获取所有子节点的ID(跳过父节点ID).如何去做
- 获取父节点具有ID的子节点
- 如何使用jQuery隐藏没有类或ID的元素…当父节点也没有id时