如何选择拥有相同ID的每个节点

发布于 2024-09-03 07:59:08 字数 246 浏览 9 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

狂之美人 2024-09-10 07:59:08

ID 必须在文档中是唯一的,所以我假设您需要这样做,因为您从某个地方获取数据并且需要清理它。如果可以的话,请解决问题的根源。

不过,如果不能,您可以循环遍历树中的元素来查找匹配的 ID;像这样的东西:

var theTargetID = /* ...whatever ID you're looking for... */;
$(theTree).find("*").each(function(element) {
    if (this.id == theTargetID) {
        // it matches the ID
    }
});

这将创建一个可能很大的临时数组(匹配树的所有后代元素)。这可能是您最好使用无聊的老式 DOM 遍历而不是 jQuery 的漂亮包装器的地方,因为您正在尝试使用无效的文档结构(多个 ID)执行某些操作。

寻找目标 ID 的原始 DOM 遍历可能如下所示:

function traverse(theTargetID, element) {
    var node;

    if (element.id == theTargetID) {
        // It matches, do something about it
    }

    // Process child nodes
    for (node = element.firstChild; node; node = node.nextSibling) {
        if (node.nodeType === 1) {  // 1 == Element
            traverse(theTargetID, node);
        }
    }
}

假设 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:

var theTargetID = /* ...whatever ID you're looking for... */;
$(theTree).find("*").each(function(element) {
    if (this.id == theTargetID) {
        // it matches the ID
    }
});

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:

function traverse(theTargetID, element) {
    var node;

    if (element.id == theTargetID) {
        // It matches, do something about it
    }

    // Process child nodes
    for (node = element.firstChild; node; node = node.nextSibling) {
        if (node.nodeType === 1) {  // 1 == Element
            traverse(theTargetID, node);
        }
    }
}

That assumes that the element argument is actually a DOM element (not a jQuery object, or a text node, etc.). It checks the element's id 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.

空心空情空意 2024-09-10 07:59:08

TJ Crowder 已经说过,ID 在文档中必须是唯一的。我认为如果存在重复的 ID,您的 jsTree 中可能会出现非常奇怪的效果,因此我建议您执行以下操作。

对于您单击的每个节点,将 id 属性的值存储在以下示例中的 var nodeId 中。示例代码将为您查找 var nodeId 的重复项。如果发现重复项,则除第一个找到的节点之外的所有节点都应将 id 更改为唯一 id。您可以通过将 i 的值或随机文本字符串附加到 id 来实现此目的。

我现在能为你做的就是这些了。如果您可以向我们提供一些更详细的信息(HTML 和您当前的 Javascript 代码),那将会有所帮助。

var nodeId = 'the-node-id'; // The id of your node id here.
$('#' + nodeId).each(function() {
  var matchingIds = $('[id='+this.id+']'); // May find duplicate ids.
  if (matchingIds.length > 1 && matchingIds[0] == this) {
    // Duplicates found.
    for (i = 0; i < matchingIds.length; i++) {
      // Whatever you like to do with the duplicates goes here. I suggest you give them new unique ids.
    }
  }
});

更新:这是一种替代解决方案,在页面加载后直接找到重复的 ID,类似于 TJ Crowder 的建议。

$('[id]').each(function() { // Selects all elements with ids in the document.
  var matchingIds = $('[id='+this.id+']'); // May find duplicate ids.
  if (matchingIds.length > 1 && matchingIds[0] == this) {
    // Duplicates found.
    for (i = 0; i < matchingIds.length; i++) {
      // Whatever you like to do with the duplicates goes here. I suggest you give them new unique ids.
    }
  }
});

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 of var 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 of i 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.

var nodeId = 'the-node-id'; // The id of your node id here.
$('#' + nodeId).each(function() {
  var matchingIds = $('[id='+this.id+']'); // May find duplicate ids.
  if (matchingIds.length > 1 && matchingIds[0] == this) {
    // Duplicates found.
    for (i = 0; i < matchingIds.length; i++) {
      // Whatever you like to do with the duplicates goes here. I suggest you give them new unique ids.
    }
  }
});

Update: This is an alternative solution where the duplicated ids are found directly after page load, similiar to T.J Crowder's suggestion.

$('[id]').each(function() { // Selects all elements with ids in the document.
  var matchingIds = $('[id='+this.id+']'); // May find duplicate ids.
  if (matchingIds.length > 1 && matchingIds[0] == this) {
    // Duplicates found.
    for (i = 0; i < matchingIds.length; i++) {
      // Whatever you like to do with the duplicates goes here. I suggest you give them new unique ids.
    }
  }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文