selecting root element in jquery

后端 未结 9 636
粉色の甜心
粉色の甜心 2021-01-18 04:06

I need to be able to select the root element from a fragment without knowing the node types, class, id or hierachy.

相关标签:
9条回答
  • 2021-01-18 04:47

    you could look into using the parent() and children() functions. is you need to go multiple levels , you can chain them like so.

    $("#a01").parent().parent()  //returns <div id="0">
    

    please see my comment, and I'll try to give a better solution.

    0 讨论(0)
  • 2021-01-18 04:51

    I may be misunderstanding the question, but assuming by root you mean the point at which the parent is a different tag to the child then the following should work.

    function GetRoot(element) {
        while(element.parent().attr("tagName") == element.attr("tagName")) {
            element = element.parent();
        }
    
        return element;
    }
    

    This basically walks up the tree until it finds a parent that is a different tag and then returns the item. For your example that would mean that if your element was in a , , or whatever it would detect it as the root and return it.

    Making a custom jquery selector with this should be possible too, but is obviously more complicated.

    It should also be fairly easy to extend this to take an integer that defines the level. All you'd need to do is walk down the tree to the specified depth once you've found the root and return those elements.

    0 讨论(0)
  • 2021-01-18 04:52

    Whilst this question was answered a while ago perfectly correctly - I was just trying to attempt this myself and wasn't too keen on using a selector like *:not(* *) -- mainly because of the probable overheads. I don't know if this is a recent addition to jQuery's ability (i'm using 1.8) but you can use:

    $(':eq(0)');
    

    This will select the first found non-textnode element, so unless the dom you are traversing is illegal in it's formation, you should always get the root element. It is also highly optimal because jQuery should know to stop after the first element found.

    So here's a little something for all of those WSOD fans out there:

    $(':eq(0)').remove();
    

    Oh and just in case it isn't obvious that the above code snippets were just illustrating the selector, and you find yourself working with a partial fragment (rather than the full document), then you should use this selector with jQuery's filter method:

    $(fragment).filter(':eq(0)');
    

    However, when working with fragments you can just do the following:

    $(fragment).get(0);
    

    Although bear in mind that .get(0) can select text/comment nodes if they are the first item in your document, whereas the filter approach will always find the first actual element.

    0 讨论(0)
提交回复
热议问题