简体   繁体   English

<a href="tel:...">在移动和非移动浏览器中使用</a>

[英]Using <a href="tel:..."> in both mobile & non-mobile browsers

I need to build a web page for mobile device.我需要为移动设备构建一个网页。

There's only one thing I still haven't figured out: how can I trigger a phone call through the click of an image.只有一件事我还没有弄清楚:如何通过单击图像来触发电话。

If I give a <a href="tel:xxx-xxx"> , it will work, but if it is clicked from non-mobile browsers, "Page not found" will show up, as the telephone number naturally isn't an existing page.如果我给一个<a href="tel:xxx-xxx"> ,它会工作,但如果从非移动浏览器点击它,“找不到页面”会出现,因为电话号码自然不是现有页面。

In wich Browser did you test that? 在浏览器中你测试过吗? I've tried it in Firefox, Chrome and IE and all of them ask me wich software they should use for "tel" links, none of them gave me an error Page. 我已经在Firefox,Chrome和IE中尝试过了,所有人都问我应该使用哪些软件用于“tel”链接,他们都没有给我一个错误页面。

Because of that fact I decided for my last made website to have the tel links in mobile and desktop browser. 因为这个事实,我决定让我上次制作的网站在移动和桌面浏览器中拥有电话链接。 Some users may use a phone software, so a call by click can be good for desktop browsers too. 有些用户可能会使用手机软件,因此点击通话对桌面浏览器也有好处。

(This answer should be a comment, but my reputation is to low) (这个答案应该是评论,但我的声誉很低)

You can try it like this: 你可以这样试试:

var isMobile = {
    Android: function() {
        return navigator.userAgent.match(/Android/i);
    },
    BlackBerry: function() {
        return navigator.userAgent.match(/BlackBerry/i);
    },
    iOS: function() {
        return navigator.userAgent.match(/iPhone|iPad|iPod/i);
    },
    Opera: function() {
        return navigator.userAgent.match(/Opera Mini/i);
    },
    Windows: function() {
        return navigator.userAgent.match(/IEMobile/i);
    },
    any: function() {
        return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows());
    }
};

and let jQuery do the rest for you: 让jQuery为你做剩下的事情:

if( isMobile.any() ){
    $('a').attr('href', 'tel:1323456458897');
}

SIDENOTE: 边注:

To specify which <a> should be affected, give your <a> an id and do it like this: 要指定哪个<a>应该受到影响,请为您的<a>指定ID,并按以下方式执行操作:

 if (isMobile.any()) {
    $('a#phonea').attr('href', 'tel: 4515715151456');
 }

I use it like this, to disable the complete link when not on mobile: (id phone is in my case a <li> element 我这样使用它,当不在移动设备上时禁用完整的链接:(在我的情况下,id电话是<li>元素

else {
    $('#phone').html('<i class="fa fa-phone"></i> Phone: 4515415411818');
}

I've setup a little fiddle with a button to show: http://jsfiddle.net/rp8ma5oe/ 我设置了一个小小提琴按钮显示: http//jsfiddle.net/rp8ma5oe/

它正在工作,就我而言,问题可能出在缓存中

它很简单:把你的链接放在下面:

<a href="tel:12345565444">something or your number</a>

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

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