Match event.target with existing jQuery object

前端 未结 4 2053
走了就别回头了
走了就别回头了 2020-12-31 14:38

How can I do that?

event.target returns a HTML object,

and my element is a jQuery object.

Is there a better way to find out if event.tar

4条回答
  •  独厮守ぢ
    2020-12-31 14:41

    You can get the actual DOM element from the jQuery using .get(0) or simply the_element[0]. It would probably be better to check with jQuery, though.

    if (the_element.is(event.target))
    {
        ...
    }
    

    Using your example:

    $(document).click(function(event){
      if (the_element.is(event.target)) {
          return false;
      }
      if(($(event.target).parents().index(box) == -1) 
        && box.is(':visible')){
          close(); 
      }
    
    });
    

提交回复
热议问题