Check if an element is a child of a parent

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

I have the following code.



    

        
7条回答
  •  执笔经年
    2020-12-07 20:27

    .has() seems to be designed for this purpose. Since it returns a jQuery object, you have to test for .length as well:

    if ($('div#hello').has(target).length) {
       alert('Target is a child of #hello');
    }
    

提交回复
热议问题