简体   繁体   English

jquery event.target is_a_child_of(element)

[英]jquery event.target is_a_child_of(element)

Given element , a variable containing a javascript object/DOM element, how do I determine if the event.target is an element inside element or not? 给定element ,一个包含javascript对象/ DOM元素的变量,如何确定event.target是否是元素内部的element

function(event){
   // assume that var element exists in this scope
   if(event.target == a_child_of(element))
      // do something
}


<div id="myDiv">
   <div class="innerDiv">
      <input type="text"/>
   </div>
</div>

If element is myDiv , an event occurring on the inner div or the input, or any other element that may exist inside myDiv should cause the statement to evaluate to true. 如果elementmyDiv ,则内部div或输入上发生的事件或myDiv中可能存在的任何其他元素应导致该语句计算为true。

I imagine that I could use a recursive function to build an array of child elements and then check if the event.target is in the array, but I want to see if there's a simpler answer first. 我想我可以使用递归函数来构建子元素数组,然后检查event.target是否在数组中,但我想先看看是否有更简单的答案。

You can also use .has() 你也可以使用.has()

if ($(element).has(e.target).length > 0){
//
}

jQuery救援

if (jQuery.contains(element, e.target))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM