简体   繁体   English

移动 Safari - 触摸事件中的 event.target

[英]Mobile Safari - event.target in touch event

I would like to have the target when a touch something on the screen of my iPad.当我触摸 iPad 屏幕上的某些东西时,我想拥有目标。 But unfortunately it always returns the currentTarget , never the target.但不幸的是,它总是返回currentTarget ,而不是目标。

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
  }

}

But event.target.tagName is always "HTML" .event.target.tagName总是"HTML" But never the real element I touched, for example an anchor tag or a div.但我从来没有接触过真正的元素,例如锚标签或 div。

How do I get the real element I touched?我如何获得我触摸的真实元素?

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.您可以尝试“event.touches”数组,该数组将为当前按下的每个手指对应一个条目,“target”值引用所接触的 DOM 元素。

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

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.当我尝试时, event.target 按预期引用了触摸的 div。 You might also check your CSS and/or JS plugins that might be interfering with event bubbling.您还可以检查可能会干扰事件冒泡的 CSS 和/或 JS 插件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM