Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/401.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 在iOS设备上返回未定义的jQuery.closest()方法_Javascript_Jquery_Ios - Fatal编程技术网

Javascript 在iOS设备上返回未定义的jQuery.closest()方法

Javascript 在iOS设备上返回未定义的jQuery.closest()方法,javascript,jquery,ios,Javascript,Jquery,Ios,jQuery方法在iOS设备上不起作用。我正在使用.closest()和event.target来检测被点击的目标元素是否具有特定的祖先。不幸的是,这会在iOS设备上返回错误 HTML <ul class="level-1"> <li><a href="#" class="item-1">Level 1 - Item 1</a></li> <li><a href="#" class="item-2"&

jQuery方法在iOS设备上不起作用。我正在使用
.closest()
event.target
来检测被点击的目标元素是否具有特定的祖先。不幸的是,这会在iOS设备上返回错误


HTML

<ul class="level-1">
    <li><a href="#" class="item-1">Level 1 - Item 1</a></li>
    <li><a href="#" class="item-2">Level 1 - Item 2</a></li>
    <li><a href="#" class="item-3">Level 1 - Item 3</a></li>
</ul>
错误



可以在中找到实例。

您正在调用dom元素的(
e.target
是dom元素引用),这是一个实验性功能(仅由a支持),需要调用jQuery方法以实现跨浏览器兼容性

console.log($(e.target).closest('.level-1'));
您还可以使用“$(this)”查找最近的元素。它基本上返回一个jquery对象来调用jquery的最近的函数

$('.item-1').on('touchstart', function(e) {
  e.preventDefault();
  console.log($(this).closest('.level-1'));
});
$('.item-1').on('touchstart', function(e) {
  e.preventDefault();
  console.log($(this).closest('.level-1'));
});