[英]preventDefault on anchor tag with tel: doesnt work
When a user clicks on a telephone number it should do nothing on desktop and on mobile it should call the number. 当用户单击电话号码时,它在台式机上不应该执行任何操作,而在移动电话上,它应呼叫该号码。 I thought the solution would be as simple as returning false or preventDefault when on a desktop but so far it doesnt seem to work: 我认为解决方案就像在台式机上返回false或preventDefault一样简单,但到目前为止它似乎不起作用:
<a href="tel:555555">555-555</a>
I'm using mouseup()
so that it only prevents it on desktop. 我正在使用mouseup()
以便仅在桌面上阻止它。 The function runs but the browser still tries to open up another app. 该功能运行,但浏览器仍尝试打开另一个应用程序。
$('a[href^="tel"]').mouseup(function (e) {
e.preventDefault();
return false;
})
Is there a solution as simple as this that actually works or is the best method to detect for mobile and change the href attributes as seen on other questions? 是否有像这样实际可行的简单解决方案,或者是检测移动设备并更改href属性的最佳方法?
SOLUTION 解
Thanks to the answer below. 感谢下面的答案。 mouseup()
runs but can't preventDefault. mouseup()
运行,但不能阻止Default。 This was the final code using modernizr: 这是使用modernizr的最终代码:
$('a[href^="tel"]').on('click',function (e) {
if ($("html").hasClass("no-touch")) {
e.preventDefault();
}
});
You could do this: 您可以这样做:
$('.no-touch a[href^="tel"]').on('click',function (e) {
e.preventDefault();
});
If you need it to work on touch devices, and are using Modernizr (which it looks like) you could do this instead: 如果您需要它在触摸设备上运行,并且正在使用Modernizr(看起来像这样),则可以执行以下操作:
$('a[href^="tel"]').on('click',function (e) {
if (!Modernizr.touch) {
e.preventDefault();
}
... my touch code ...
});
Try like 尝试像
function callMethod(){
//do stuff here
}
$('a[href^="tel"]').on('click',function (e) {
e.preventDefault();
callMethod();
})
$('a[href^="tel"]').on('touchstart',function (e) {
callMethod();
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.