jQuery ancestors using jQuery objects

前端 未结 7 1799
北荒
北荒 2021-01-02 18:33

I\'d like to check ancestry using two jQuery objects. They don\'t have IDs, and are only going to be available as jQuery objects (or DOM nodes if you called get()

7条回答
  •  渐次进展
    2021-01-02 19:00

    One way would be to use the filter function

    $('a').click(function() {
        $(this).parents().filter(function() {
           return this == someDiv[0];
        }).each(function() {
           alert('foo');
        })
    }
    

    I think you may also be able to get away with using jQuery.inArray

    if ($.inArray( someDiv, $(this).parents() ) ) {
            alert('boo');
    }
    

提交回复
热议问题