2012-03-10 79 views
0

我试图检测用户何时触摸了网页内的链接,而不是触摸页面的任何其他部分,但它不工作 - 会发生什么是在下面的代码无论我接触到哪个链接,都会弹出“触及非链接”的提醒。检测用户何时触摸链接

这段代码有什么问题?

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

拼写错误'stopPropagation()'。 – JJJ 2012-03-10 16:04:14

+0

如果这里的帖子中没有输入错误,那应该是event.stopPropagation() - ..pa .. not ..pe .. – 2012-03-10 16:05:31

+0

也有几个地方拼错了'Listener'。 – 2012-03-11 01:04:39

回答

1

您已将touchesOnNonLinksListerner附加到您的链接中。改为附加touchesOnLinksListener!