如何选择拥有相同ID的每个节点
我有一个 Jstree,它包含很多节点,其中一些节点具有相同的 ID。
我想知道,如果有人选择我该如何做
节点之一,它会选择具有相同 id 的每个节点。
我尝试过使用,
onselect: function (node) {
但我不确定到底该怎么做,
另外我不知道如何手动选择节点
(因为这一切都是通过 selected: 属性完成的)
I have a Jstree that holds a lot of nodes, some of them have the same ID.
I was wondering, how do I make it so that if someone selects
one of the nodes, it would select every node with the same id.
I tried working with the
onselect: function (node) {
but I'm not sure what exactly to do,
plus I'm not sure how to manually select a node
(because it's all done with the selected: attribute)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
ID 必须在文档中是唯一的,所以我假设您需要这样做,因为您从某个地方获取数据并且需要清理它。如果可以的话,请解决问题的根源。
不过,如果不能,您可以循环遍历树中的元素来查找匹配的 ID;像这样的东西:
这将创建一个可能很大的临时数组(匹配树的所有后代元素)。这可能是您最好使用无聊的老式 DOM 遍历而不是 jQuery 的漂亮包装器的地方,因为您正在尝试使用无效的文档结构(多个 ID)执行某些操作。
寻找目标 ID 的原始 DOM 遍历可能如下所示:
假设
element
参数实际上是 DOM 元素(不是 jQuery 对象或文本节点等)。它检查元素的id
,然后处理其子元素,如果需要的话会递归地处理。这可以避免创建可能很大的数组。请注意,我指的是树节点,而不是其中的叶子。您希望在加载树时执行此操作一次,而不仅仅是在选择树中的节点时执行此操作 - 因为您希望尽可能简短地拥有无效结构并主动修复它。
IDs must be unique within the document, so I'm assuming you need to do this because you're getting the data from somewhere and need to clean it up. If you can, fix the source of the problem.
If you can't, though, you can loop through the elements within the tree looking for the matching ID; something like this:
That will create a potentially large interim array (matching all descendant elements of the tree). This may be a place where you're best off using boring old fashioned DOM traversal rather than jQuery's nice wrappers, since you're trying to do something with an invalid document structure (multiple IDs).
Here's what a raw DOM traversal looking for a target ID might look like:
That assumes that the
element
argument is actually a DOM element (not a jQuery object, or a text node, etc.). It checks the element'sid
and then processes its children, recursively if necessary. This avoids creating a potentially-large array.Note that I've been referring to the tree node, not a leaf within it. You want to do this once, when the tree is loaded, not only when a node within the tree is selected — because you want to have an invalid structure as briefly as possible and fix it proactively.
TJ Crowder 已经说过,ID 在文档中必须是唯一的。我认为如果存在重复的 ID,您的 jsTree 中可能会出现非常奇怪的效果,因此我建议您执行以下操作。
对于您单击的每个节点,将 id 属性的值存储在以下示例中的
var nodeId
中。示例代码将为您查找var nodeId
的重复项。如果发现重复项,则除第一个找到的节点之外的所有节点都应将 id 更改为唯一 id。您可以通过将i
的值或随机文本字符串附加到 id 来实现此目的。我现在能为你做的就是这些了。如果您可以向我们提供一些更详细的信息(HTML 和您当前的 Javascript 代码),那将会有所帮助。
更新:这是一种替代解决方案,在页面加载后直接找到重复的 ID,类似于 TJ Crowder 的建议。
A T.J Crowder already have said, IDs must be unique within a document. I think you can end up with a very strange effect in your jsTree if there are duplicated IDs, so I would recommend that you do the following.
For each node you're clicking on, store the value of the id attribute in
var nodeId
in the example below. The example code will find duplicates ofvar nodeId
for you. If you find duplicates, then all but the first found node should have the id changed to a unique id. You can do that by appending the value ofi
or a random text string to the id.That's all I can do for you now. If you could provide us with some more detailed information (HTML and your current Javascript code) that would help.
Update: This is an alternative solution where the duplicated ids are found directly after page load, similiar to T.J Crowder's suggestion.