jquery event.target is_a_child_of(element) [英] jquery event.target is_a_child_of(element)
问题描述
给定元素
,一个包含javascript对象/ DOM元素的变量,如何确定event.target是否是元素<中的元素/ code>或不是?
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?
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>
如果元素
是 myDiv
,内部div或输入上发生的事件,或 myDiv
中可能存在的任何其他元素应导致语句评估为是的。
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.
我想我可以使用递归函数来构建子元素数组,然后检查event.target是否在数组中,但我想看看是否有一个更简单的答案。
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.
推荐答案
if (jQuery.contains(element, e.target))
这篇关于jquery event.target is_a_child_of(element)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!