如何获取子元素的 id/

How can I get the id of a child element/

本文关键字:元素 id 何获取 获取      更新时间:2023-09-26

我正在尝试获取子li元素的id,并查看该id是否包含单词"unit"。这是我一直在研究的javascript部分:

insertSearchIcon: function(evt){
    var treeExists = $( '.jstree-icon' ).html();
    var currentTarget = $(evt.currentTarget);
    if (treeExists != null){                                                            //Check to see if tree exists
        var elementExists = document.getElementsByClassName( 'oob-dropdown' );
        zero = 0;
        if(elementExists.length == zero){                                                       //Check to see if button is already there
            var idOfParent = currentTarget.siblings('li').id;
            if(idOfParent.indexOf("unit") != -1){                                       //Check to see if element is not a unit
                currentTarget.$( '.jstree-leaf' ).prepend('<button class="oob-dropdown" id="oob-button"></button>');
            }
        }
    }
}

我正在尝试获取与单击的".jstree-icon"下的".jstree-leaf"一起使用的 id:

<li id="unit-27404752" class="jstree-last jstree-open">
 <ins class="jstree-icon"> </ins><!--this one was clicked--> 
  <a href="#">
   <ul style="">
    <li id="unit-27404753" class="jstree-closed">
    <li id="unit-27404754" class="jstree-closed">
    <li id="unit-27404755" class="jstree-leaf">
    <ins class="jstree-icon"> </ins> <--!Not this one>
    <a class="" href="#">
    <ins class="jstree-icon" style="background: url("/api/mil2525/images/SFGPUUM----CUSG") no-repeat scroll center center transparent;"> </ins>
      S2 SEC 330 MED
     </a>

jQuery.find与属性和 not 选择器结合使用。有了这个,您可以获得类的所有元素,jstree-leaf其属性id不包含单词 unit .

$(".jstree-leaf:not([id*='unit'])")

然后,如果要检索其 ID 或对它们执行某些操作,可以遍历该元素:

$(".jstree-leaf:not([id*='unit'])").each(function() {
    // Do whatever you want with the id or the element.
    var id = $(this).attr('id');
    console.log("Found id: " + id);
}

尝试:

$(this).find(".jstree-leaf").each(function () { console.log($(this).attr('id'))});

这实际上不是子元素,而是同级的子元素。

你可以使用类似的东西——

currentTarget.next("a").find(".jstree-leaf").whateverJQuery

此外,正如我在评论中指出的那样,示例中的 url 属性正在脱离引号,请尝试在 url 内'或根本不使用它们。

使用 $('li[id^=unit-]', '.jstree-icon') 检查li元素的 id 有多少(如果有的话)以 unit- 开头,然后在 if 语句中使用该事实:

改变:

        var idOfParent = currentTarget.siblings('li').id;
        if(idOfParent.indexOf("unit") != -1){                                       //Check to see if element is not a unit
            currentTarget.$( '.jstree-leaf' ).prepend('<button class="oob-dropdown" id="oob-button"></button>');
        }

自:

        var idUnit = $('li[id^=unit-]', '.jstree-icon');
        if( idUnit.length ) {
            currentTarget.find( '.jstree-leaf' ).prepend('<button class="oob-dropdown" id="oob-button"></button>');
        }

这就是我让它按照我想要的方式工作后函数的工作方式,感谢所有的帮助。

insertSearchIcon: function(evt){
    var treeExists = $( '.jstree-icon' ).html();
    var currentTarget = $(evt.currentTarget);
    if (treeExists != null){                                                            //Check to see if tree exists
        var elementExists = document.getElementsByClassName( 'oob-dropdown' );
        zero = 0;
        if(elementExists.length == zero){                                               //Check to see if button is already there
            $('.jstree-leaf').each(function(){                                          //If not a unit put button
                var id = this.id;
                if (id.indexOf("unit") == -1){
                    $(this).prepend('<button class="oob-dropdown" id="oob-button"></button>');
                }
            });
        }
    }
}
var a = $(".jstree-icon").next().children().children().attr("id");
if (a === "unit") {
   // do something
}