簡體   English   中英

檢測用戶何時觸摸鏈接

[英]Detecting when user touches a link

我正在嘗試檢測用戶何時觸摸了網頁內的鏈接以及何時觸摸了頁面的任何其他部分,但操作不正常-發生在以下代碼中的警報“觸摸了非鏈接”彈出了無論我觸摸的是什么,無論它是否鏈接。

這段代碼有什么問題?

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();
}

您還已將touchesOnNonLinksListerner附加到鏈接。 而是附加touchesOnLinksListener!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM