循环遍历jstree以搜索节点id的所有出现,然后更改这些节点的类 [英] loop through jstree to search for all occurences of a node id and then change the class of those nodes
问题描述
我有以下javascript代码更改了jstree中每个选定节点的类(< ins>
标记):
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
});
上面的代码工作正常,除非有一件事,如果所选的sub_id存在于多个地方树,显示新图标的类似乎不起作用。
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.
我相信我已遍历jstree来搜索sub_id的所有出现然后关联节点的新类。
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.
谢谢很多。
推荐答案
使用#id选择器时,它只会返回第一个元素。将sub_id添加到名称或类属性可以帮助您解决问题。正如我在评论中提到的,id属性在页面上应该是唯一的。
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.
您还需要删除:eq(1)如果要将类应用于所有匹配元素而不仅仅是第二个元素,则从选择器中选择
。 :eq
接受基于0的索引。
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.
修改
您的新选择器:
$j("your-element[name='"+sub_id+"' ins").attr("class","jstree-icon2");
这篇关于循环遍历jstree以搜索节点id的所有出现,然后更改这些节点的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!