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);