loop through jstree to search for all occurences of a node id and then change the class of those nodes

二次信任 提交于 2019-12-14 00:37:56

问题


I have the following javascript code which changes the class (of the <ins> tag) for each selected node in the jstree:

   $j("#actionButton1").click(function() {

      $j.each($j("#demo2").jstree("get_selected"), function(index, element) {

      alert($j(element).attr('id'));

      var sub_id = $j(element).attr('id'); //node id is stored in the varialble sub_id

      $j("#"+sub_id+" ins:eq(1)").attr("class","jstree-icon2"); // set class to display new icon

   });//end of selected nodes loop
});

The code above works fine except for one thing, if the selected sub_id exists in more than one place in the tree, the class to display a new icon does not seem to work.

I believe i have loop through the jstree to search for all occurences of the sub_id and then associate the new class to the nodes.

Any hint on how to do that is most welcomed.

Thanks a lot.


回答1:


When you use the # id selector it will only return the first element. Adding sub_id to a name or class attribute should help you with your problem. As i mentioned in the comment, the id attribute should be unique on the page.

You will also want to remove the :eq(1) from your selector if you want to apply the class to all matching elements and not just the second. :eq accepts a 0 based index.

Edit

Your new selector:

$j("your-element[name='"+sub_id+"' ins").attr("class","jstree-icon2");



回答2:


Try referencing the node directly instead of using the ID as a selector:

$j('#actionButton1').click(function() {
    $j.each($j('#demo2').jstree('get_selected'), function(index, element) {
        $j('ins:eq(1)', element).addClass('jstree-icon2');
    });
});

As mentioned in the comments above, IDs should be unique.

I hope this helps!



来源:https://stackoverflow.com/questions/8010636/loop-through-jstree-to-search-for-all-occurences-of-a-node-id-and-then-change-th

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!