Check if an element is a child of a parent

后端 未结 7 872
故里飘歌
故里飘歌 2020-12-07 19:54

I have the following code.



    

        
相关标签:
7条回答
  • 2020-12-07 20:06

    If you are only interested in the direct parent, and not other ancestors, you can just use parent(), and give it the selector, as in target.parent('div#hello').

    Example: http://jsfiddle.net/6BX9n/

    function fun(evt) {
        var target = $(evt.target);    
        if (target.parent('div#hello').length) {
            alert('Your clicked element is having div#hello as parent');
        }
    }
    

    Or if you want to check to see if there are any ancestors that match, then use .parents().

    Example: http://jsfiddle.net/6BX9n/1/

    function fun(evt) {
        var target = $(evt.target);    
        if (target.parents('div#hello').length) {
            alert('Your clicked element is having div#hello as parent');
        }
    }
    
    0 讨论(0)
  • 2020-12-07 20:06

    In addition to the other answers, you can use this less-known method to grab elements of a certain parent like so,

    $('child', 'parent');
    

    In your case, that would be

    if ($(event.target, 'div#hello')[0]) console.log(`${event.target.tagName} is an offspring of div#hello`);
    

    Note the use of commas between the child and parent and their separate quotation marks. If they were surrounded by the same quotes

    $('child, parent');
    

    you'd have an object containing both objects, regardless of whether they exist in their document trees.

    0 讨论(0)
  • 2020-12-07 20:07

    You can get your code to work by just swapping the two terms:

    if ($(target).parents('div#hello').length) {
    

    You had the child and parent round the wrong way.

    0 讨论(0)
  • 2020-12-07 20:10

    Ended up using .closest() instead.

    $(document).on("click", function (event) {
        if($(event.target).closest(".CustomControllerMainDiv").length == 1)
        alert('element is a child of the custom controller')
    });
    
    0 讨论(0)
  • 2020-12-07 20:13

    Vanilla 1-liner for IE8+:

    parent !== child && parent.contains(child);
    

    Here, how it works:

    function contains(parent, child) {
      return parent !== child && parent.contains(child);
    }
    
    var parentEl = document.querySelector('#parent'),
        childEl = document.querySelector('#child')
        
    if (contains(parentEl, childEl)) {
      document.querySelector('#result').innerText = 'I confirm, that child is within parent el';
    }
    
    if (!contains(childEl, parentEl)) {
      document.querySelector('#result').innerText += ' and parent is not within child';
    }
    <div id="parent">
      <div>
        <table>
          <tr>
            <td><span id="child"></span></td>
          </tr>
        </table>
      </div>
    </div>
    <div id="result"></div>

    0 讨论(0)
  • 2020-12-07 20:15

    If you have an element that does not have a specific selector and you still want to check if it is a descendant of another element, you can use jQuery.contains()

    jQuery.contains( container, contained )
    Description: Check to see if a DOM element is a descendant of another DOM element.

    You can pass the parent element and the element that you want to check to that function and it returns if the latter is a descendant of the first.

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