繁体   English   中英

如何获取子元素的ID /

[英]How can I get the id of a child element/

我正在尝试获取子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与属性和非选择器结合使用。 这样,您将获得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>');
        }
var a = $(".jstree-icon").next().children().children().attr("id");
if (a === "unit") {
   // do something
}

在我按期望的方式工作之后,此功能即会工作,感谢所有帮助。

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>');
                }
            });
        }
    }

}

暂无
暂无

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

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