jquery event.target is_a_child_of(element) [英] jquery event.target is_a_child_of(element)

查看:96
本文介绍了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.

推荐答案

jQuery救援

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

这篇关于jquery event.target is_a_child_of(element)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆