Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 检查事件目标是否具有特定的祖先_Javascript_Jquery - Fatal编程技术网

Javascript 检查事件目标是否具有特定的祖先

Javascript 检查事件目标是否具有特定的祖先,javascript,jquery,Javascript,Jquery,我需要检查单击的元素是否具有特定的祖先 $(document).click(function(e) { console.log(e.target); var ele = $(e.target); console.log(ele.parents('.datepicker').length > 0); } 这将产生: <td class="active">24</td> false 它输出 true $(e.target)的输出是:[td.active

我需要检查单击的元素是否具有特定的祖先

$(document).click(function(e) {
  console.log(e.target);
  var ele = $(e.target);
  console.log(ele.parents('.datepicker').length > 0);
}
这将产生:

<td class="active">24</td>
false
它输出

true
$(e.target)的输出是:
[td.active,context:td.active]
这不是我所期望的,所以我认为问题在于:
var ele=$(e.target)但我无法找出它有什么问题。

使用

console.log($(e.target).parents('.datepicker').length > 0);

编辑: 最近的()给出了true。请检查一下

使用

console.log($(e.target).parents('.datepicker').length > 0);

编辑: 最近的()给出了true。请检查一下

试试
最近的()

log($(this).closest('.datepicker')。长度>0);

假设
datepicker
在表中:

console.log($(this).closest('tr').find('.datepicker').length > 0);

请尝试
最近的()

log($(this).closest('.datepicker')。长度>0);

假设
datepicker
在表中:

console.log($(this).closest('tr').find('.datepicker').length > 0);


这两个输出错误这两个输出错误用
this
e.target
进行了尝试,所有输出为错误用
this
e.target
进行了尝试,所有输出为错误
console.log($(this).closest('table').find('.datepicker').length > 0);