2012-03-10 134 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!