[英]How to perform some action when a user touches anywhere that isn't a link/anchor etc?
[英]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.