jQuery ancestors using jQuery objects

前端 未结 7 1817
北荒
北荒 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 18:56

    You can use the index() method to check if an element exists in a list, so would the following work?

    var someDiv = $('#div');
    
    $('a').click(function() {
        if ($(this).parents().index(someDiv) >= 0) {
            alert('boo');
        }
    }
    

    From #index reference.

提交回复
热议问题