简体   繁体   English

当不在移动设备上时,如何禁用链接以拨打电话号码?

[英]How to disable a link to call a phone number when not on mobile devices?

What I would like to build is a button that will allow the user to call in. On desktop this element will look like a big call-to-action, 我要构建的是一个按钮,允许用户调用。在桌面上,该元素看起来像是一个大型号召性用语,

在桌面上

then on mobile this will be both a call-to-action and an actual method to call. 那么在移动设备上,这既是号召性用语,又是一种实际的调用方法。

在移动

The challenge that I need to address now is that the ability to call should be only present when on mobile. 我现在要解决的挑战是,只有在移动设备上才能显示呼叫功能。 This is my current code: 这是我当前的代码:

<div id="headerCTA">
  <div>
    <a href="tel:888-336-1301">
      <div>
        Schedule a pickup<br>
        888-336-1301
      </div>  
    </a>
  </div>
</div> 

[Code gist on GitHub - for additional commenting] [GitHub上的代码要点-附加评论]

But when on desktop (dictated by screen width?) this link should be deactivated. 但是在桌面上(由屏幕宽度决定?)时,应禁用此链接。 At the moment if someone happens to click on this from desktop, it attempts to open a page as "tel:888-336-1301" 此刻,如果有人碰巧从桌面上单击它,它会尝试以“ tel:888-336-1301”打开页面

Which produces the following error: 产生以下错误:

The address wasn't understood Firefox doesn't know how to open this address, because the protocol (tel) isn't associated with any program. 无法理解该地址,Firefox不知道如何打开该地址,因为协议(tel)与任何程序都没有关联。

Another idea, that doesn't involve JavaScript, is to use CSS pointer-events property. 另一个不涉及JavaScript的想法是使用CSS pointer-events属性。 Use media query to target devices that may not have the ability to make calls and for them, set pointer-events property to none . 使用媒体查询将目标定位为可能无法进行呼叫的设备,并将它们的pointer-events属性设置为none This way, clicking on the link won't have any effect. 这样,单击链接不会有任何效果。

Your code could look like this: 您的代码可能如下所示:

@media only screen 
and (min-device-width : 768px){
  #headerCTA a{
   pointer-events: none;
  }
}

See pointer-events property documentation for details. 有关详细信息,请参见pointer-events属性文档

Use a mobile detection script, like the one in the accepted answer here: Detecting a mobile browser 使用移动检测脚本,如此处接受的答案中的脚本: 检测移动浏览器

Then 然后

var mobile = mobilecheck();
if (!mobile) {
  document.getElementById('telephone-link').href = '#';
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM