简体   繁体   English

如何获取UL中所有子节点UL标签的ID

[英]How to get the id of all the child node UL tags in a UL

I know this is a pretty basic question I just seem to be having some issues doing it. 我知道这是一个非常基本的问题,我似乎在执行时遇到了一些问题。 I have a HTML structure like below. 我有一个如下的HTML结构。

<ul>
    <li>
        <ul class=t2 id=15>
            <li class='item'>a<span class='val'>b</span></li>
            <li class='item'>c<span class='val'>d</span></li>
            <li class='item'>e<span class='val'>f</span></li>
            <li class='item'>parameters : </li>
            <li>
                <ul class=t3 id=16>
                    <li>
                        <ul class=t4 id=17></ul>
                    </li>
                    <li>
                        <ul class=t4 id=18></ul>
                    </li>
                </ul>
            </li>
        </ul>
    </li>
</ul>

I have the UL with the id of 16 selected and I want to select all its child ul nodes and grab there id. 我选择了ID为16的UL,并且我想选择其所有子ul节点并在那里获取ID。 I am able to select the ul with an id of 17 but I cannot grab it's sister node. 我可以选择ID为17的ul,但无法抓住它的姊妹节点。 Here is the JavaScript I am using to get the child nodes. 这是我用来获取子节点的JavaScript。

if (document.getElementById(this.toDelete[i]).getElementsByTagName('ul').length >= 1) {
    var tag = document.getElementById(this.toDelete[i]).getElementsByTagName('ul');
        for (var k = 0; k <= tag.length; k++) {
            console.log("tag name: " + tag[k].id + " these will be pushed to Delete");
        }
}

So the logic should be, the the selected UL has child ULs get the ID of those ULs and print them to the console. 因此逻辑应该是,所选的UL具有子UL,以获取这些UL的ID并将其打印到控制台。

The above code does not work. 上面的代码不起作用。 I believe that is because it is grabbing the which does not have a id. 我认为这是因为它正在抓取没有ID的。 But it also if I change it to k < tag.length it works, but still only gets 17 and I want to it get 18 as well. 但是,如果我将其更改为k <tag.length,它也可以工作,但仍然只能得到17,而我也希望得到18。

Please help. 请帮忙。 Thanks in advance. 提前致谢。

UPDATED, full function. 更新,功能齐全。 the items array is an array of objects with html and id properties, toDelete is an array with just numbers (ids of items to be deleted. The html in items.html corresponds to one line of html. IE 'ab'. The function is a bit of a mess since I am just trying to get it to work properly. I know I can make it cleaner, that is why I did not post the whole function. items数组是具有html和id属性的对象的数组,toDelete是仅包含数字(要删除的项目的id的数组。items.html中的html对应于html的一行。IE'ab'。该函数是有点混乱,因为我只是试图使其正常工作,我知道我可以使其更整洁,这就是为什么我没有发布整个功能。

deleteItems: function () {
    for (var i = 0; i < this.toDelete.length; i++) {
        console.log("Item to be deleted: " + this.toDelete[i]);
        for (var j = 0; j < this.items.length; j++) {
            if (this.items[j].id == this.toDelete[i]) {
                this.items[j] = ""; //this should be a slice
                if (document.getElementById(this.toDelete[i]).getElementsByTagName('ul').length >= 1) {
                    var tag = document.getElementById(this.toDelete[i]).getElementsByTagName('ul');
                    for (var k = 0; k <= tag.length; k++) {
                        console.log("tag name: " + tag[k].id + " these will be pushed to Delete");
                    }
                    this.toDelete.push(document.getElementById(this.toDelete[i]).getElementsByTagName('ul')[0].id);
                    //check to see if it has those there sister nodes. 
                }
            }
        }
    }
},

You can use children to get each child node, and then you can check to ensure the found node is a ul by using JavaScript's nodeName . 您可以使用children得到每个子节点,然后就可以进行检查以确保找到的节点是ul使用JavaScript的nodeName To be extra safe I used toLowerCase() to guarantee that the nodeName looks like ul and not UL 为了更加安全,我使用了toLowerCase()来确保nodeName看起来像ul而不是UL

deletedItems();

function deletedItems() {
    var ulChildren = document.getElementById('ul16').children;                
    var childrenLength = ulChildren.length;
alert(childrenLength);    
    for(var i = 0; i < childrenLength; i++){
        if(ulChildren[i].children[0].nodeName.toLowerCase() === 'ul'){
            alert("found one, the id is: " + ulChildren[i].children[0].id);
        }
    }
}

Live Example 现场例子

Also, I modified your HTML a bit: 另外,我对HTML进行了一些修改:

<ul>
    <li>
        <ul class=t2 id=15>
            <li class='item'>a<span class='val'>b</span></li>
            <li class='item'>c<span class='val'>d</span></li>
            <li class='item'>e<span class='val'>f</span></li>
            <li class='item'>parameters : </li>
            <li>
                <ul class="t3" id="ul16">
                    <li>
                        <ul class=t4 id="ul17"></ul>
                    </li>
                    <li>
                        <ul class="t4" id="ul18"></ul>
                    </li>
                </ul>
            </li>
        </ul>
    </li>
</ul>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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