简体   繁体   English

检测用户何时触摸链接

[英]Detecting when user touches a link

I'm trying to detect when the user has touched a link within a web page versus when they have touched any other part of the page, but its not working - what happens is in the following code the alert "touched a non link" pops up wherever I touch, regardless of if its a link. 我正在尝试检测用户何时触摸了网页内的链接以及何时触摸了页面的任何其他部分,但操作不正常-发生在以下代码中的警报“触摸了非链接”弹出了无论我触摸的是什么,无论它是否链接。

What is there a problem with this code? 这段代码有什么问题?

function addListeners()
{  
    alert('adding listeners');

    // Attach the listener for touches on non-links to the document node
    document.addEventListener("touchstart", touchesOnNonLinksListerner, false);

    // Attach the listener for touches on links to the anchor nodes
    var links = document.getElementsByTagName("a");
    for (var index = 0; index < links.length; ++index)
    {
        links[index].addEventListener("touchstart", touchesOnNonLinksListerner, false);
    }
}; 

function touchesOnNonLinksListerner(event)
// Catches touches anywhere in the document
{
    alert("touched  a non link");
}

function touchesOnLinksListener(event)
// Listens for touches which occur on links, then prevents those touch events from bubbling up to trigger the touchesOnNonLinksListerner
{
    alert("touched a link");
    if (typeof event == "undefined")
    {
        event = window.event;
    }

    event.stopPropegation();
}

You have attached touchesOnNonLinksListerner to your links as well. 您还已将touchesOnNonLinksListerner附加到链接。 Attach touchesOnLinksListener instead! 而是附加touchesOnLinksListener!

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

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