如何获取子元素的id /

时间:2015-01-16 16:51:06

标签: javascript jquery

我正在尝试获取子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>');
            }
        }
    }

}

我正在努力获得伴随着&#34; .jstree-leaf&#34;在&#34; .jstree-icon&#34;点击了:

<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>

6 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

尝试:

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

答案 2 :(得分:0)

这实际上不是一个子元素,它是兄弟元素的元素。

您可以使用类似 -

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

另外,正如我在评论中指出的那样,示例中的url属性是突破引号,在url中尝试'或根本不使用它们。

答案 3 :(得分:0)

使用$('li[id^=unit-]', '.jstree-icon')检查li元素的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>');
        }

答案 4 :(得分:0)

var a = $(".jstree-icon").next().children().children().attr("id");
if (a === "unit") {
   // do something
}

答案 5 :(得分:0)

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

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

}