javascript - Mobile Safari - event.target in touch event - Stack Overflow

I would like to have the target when a touch something on the screen of my iPad.But unfortunately it a

I would like to have the target when a touch something on the screen of my iPad. But unfortunately 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
  }

}

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?

I would like to have the target when a touch something on the screen of my iPad. But unfortunately 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
  }

}

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?

Share edited Oct 11, 2020 at 11:26 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Mar 6, 2012 at 8:35 VinzcentVinzcent 1,4486 gold badges33 silver badges59 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

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
    }
}

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744907999a4600373.html

相关推荐

  • javascript - Mobile Safari - event.target in touch event - Stack Overflow

    I would like to have the target when a touch something on the screen of my iPad.But unfortunately it a

    1天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信