移动Safari - 触摸事件中的event.target [英] Mobile Safari - event.target in touch event

查看:63
本文介绍了移动Safari - 触摸事件中的event.target的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在我的iPad屏幕上触摸某个东西时有目标。
但不幸的是它总是返回currentTarget,而不是目标。

I would like to have the target when a touch something on the screen of my iPad. But unfortunatly it always returns the currentTarget, never the target.

document.addEventListener('touchstart', onDocumentTouchStart, false);

function onDocumentTouchStart(event) {

  if (event.target.tagName.toLowerCase() == "div" || event.target.tagName.toLowerCase() == "canvas") {
      // do someting
  }
  else
  {
     // do something else
  }

}

但是 event.target.tagName 总是HTML。但绝不是我触及的真实元素,例如锚标记或div。

But event.target.tagName is always "HTML". But never the real element I touched, for example an anchor tag or a div.

如何获得我触及的真实元素?

How do I get the real element I touched?

非常感谢。

Vincent

推荐答案

您可以尝试event.touches数组,当前每个手指都有一个条目,其中target值引用了触及的DOM元素。

You might try the "event.touches" array, which will have one entry for each finger currently down, with the "target" value referencing the DOM element touched.

function onDocumentTouchStart(event) {
    if (event.touches[0] && event.touches[0].target.tagName.toLowerCase() == "div") {
        // do someting
    }
}

但是,我应该注意到我无法复制原始问题;当我尝试它时,event.target按照预期将触摸的div称为。您可能还会检查可能干扰事件冒泡的CSS和/或JS插件。

However, I should note that I was unable to duplicate the original issue; when I tried it, event.target referred to the touched div as intended. You might also check your CSS and/or JS plugins that might be interfering with event bubbling.

这篇关于移动Safari - 触摸事件中的event.target的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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