开发者

How to select every node that holds the same ID

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 wor开发者_JS百科king 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)


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.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜