我正在尝试检测用户何时触摸了网页中的链接,而不是他们触摸了网页的任何其他部分,但是它无法正常工作 - 以下代码中发生的情况是“触及非链接“无论我触摸到哪里都会弹出。
此代码有什么问题?
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();
}
答案 0 :(得分:1)
您已将touchesOnNonLinksListerner附加到您的链接。改为附上touchesOnLinksListener!