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 badges1 Answer
Reset to default 4You 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
评论列表(0条)