如何在网站上添加一个可点击的电话号码,但在不支持触控的网站浏览时隐藏链接。
我可以使用 Modernizr 来设置它,但是我不知道如何做。
<a href="tel:1300723579"><p><img src="assets/images/bt_calltoaction.gif" alt="View Projects" width="306" height="60"></p></a>
你能把代码复制一份吗?也就是说...
<div class="desktoptel">0800 000 000</div>
<div class="mobiletel"><a href="tel:0800-000-000">0800-000-000</div>
那么根据你的浏览器大小,只需对相应的类使用'display:none;'即可。
<p>Just call us at <a class="cssclassname" href="tel:18005555555"
title="CLICK TO DIAL - Mobile Only">(800) 555-5555</a>.</p>
tel:
链接的可靠方法。 - Adam Leggett对我来说,最简单的方法是使用CSS,而不需要任何新的类/设置:
a{
color: #3399ff;
}
a[href^="tel"]:link,
a[href^="tel"]:visited,
a[href^="tel"]:hover {
text-decoration: none;
color: #000;
pointer-events: none;
cursor: default;
}
/* Adjust px here (1024px for tablets maybe) */
@media only screen and (max-device-width: 480px) {
a[href^="tel"]:link,
a[href^="tel"]:visited,
a[href^="tel"]:hover {
text-decoration: underline;
color: #3399ff;
pointer-events: auto;
cursor: pointer;
}
}
HTML就像这样:
<a href="tel:+123-456-7">(+12)3 456 7</a>
var msie = window.navigator.userAgent.indexOf("MSIE ");
if (msie > 0){
var Elems = [], Tags = document.querySelectorAll("a[href^='tel']");
//Nodelist to array, so we're able to manipulate the elements
for (var i = 0; i < Tags.length; i++ ) {
Elems[ i ] = Tags[ i ];
}
for(var i = 0; i < Elems.length; i++){
Elems[ i ].removeAttribute('href');
}
}
编辑:我在另一个帖子上找到了另一个答案,可能对你有用 - SO - Answer
@media(min-width:768px){
a[href^="tel:"] {
pointer-events: none;
}
}
如果屏幕宽度小于768像素,该元素将作为链接,而在大于此宽度时则仅作为文本显示。
我最近也遇到了同样的问题。这个问题在stackoverflow和其他地方都很常见。如何隐藏'tel:'前缀并防止它在常规浏览器中出现错误。没有一个好的单一答案。
最终,我是这样做的:
首先,我使用元语言根据用户代理字符串来过滤浏览器与移动设备(如php / coldfusion / perl):
regular exp match for "/Android|webOS|iPhone|iPad|BlackBerry/i",CGI.HTTP_USER_AGENT
这段文字给出了一个针对桌面浏览器和手机的if/else条件语句。
接下来,我的href标签看起来像这样:<a class="tel" id='tel:8005551212' href=''>800-555-1212</a>
使用CSS在桌面样式表中为.tel类设置样式,使其对桌面浏览器不像链接。电话号码仍然可以被点击,但不是很明显,也不会有任何作用:
/* this is in default stylesheet, styles.css: */
.tel{
text-decoration:none;
color: #000;
cursor:default;
}
/* it should be re-styled in mobile css: */
.tel{
text-decoration: underline;
color: #0000CC;
cursor:auto;
}
最后,我对移动链接做了一些jquery处理。jQuery从a.tel类中获取id,并将其插入href属性中,这使得电话用户可以点击它。
整个过程看起来像这样:
<!-- get regular CSS -->
<link rel="stylesheet" href="styles/styles.css" type="text/css" media="Screen" />
<!-- get user agent in meta language. and do if/else on result.
i'm not going to write that part here, other than pseudocode: -->
if( device is mobile ) {
<!-- load mobile CSS -->
<link rel="stylesheet" href="styles/mobile.css" type="text/css" media="handheld" />
<!-- run jQuery manipulation -->
<script>
$(function(){$('a.tel').prop('href',$('a.tel').prop('id'));});
</script>
}
<p> Call us today at <a class="tel" id='tel:8005551212' href=''>800-555-1212</a>!</p>
这种方法有一个需要注意的地方:id应该是唯一的。如果你在页面上有重复的电话号码需要链接,可以将id改为name,然后使用jQuery循环遍历它们。
data-*
属性比 ID 更好 - 实际上,它们就是为此目的而设计的。 - Jeremy J Starcherif(typeof window.orientation !== 'undefined'){
$('a[href^="tel:"]').on('click', function(e){
e.preventDefaults();
});
}
我找到了最好的方法。我知道这已经过时了,但我找到了一种非常简单的方法。
使用下面的代码:
<a href=“tel:888-555-5555” class="not-active">888-555-5555</a>
//This is the logic you will add to the media query
.not-active {
pointer-events: none;
cursor: default;
}
@media only screen and (min-width: 64em) {
/* add the code */
.not-active {
pointer-events: none;
cursor: default;
}
}
对于大多数浏览器,使用简单的媒体查询即可实现此目标。以下代码对我来说非常有效:
<style type="text/css">
#mobil-tel {
display:none;
}
@media (max-width: 700px) {
#mobil-tel {
display:block;
}
#desktop-tel{
display:none;
}
}
</style>
touch
测试。它并不是一个完美的解决方案,因为它无法帮助平板电脑或触摸笔记本电脑,但在大多数桌面浏览情况下都能正常工作。请拨打我们的电话:<a href="tel:1-800-BOLOGNA" class="call-us">1-800-BOLOGNA</a>
CSS代码:.no-touch a.call-us {
color: black; /* use default text color */
pointer-events: none; /* prevents click event */
cursor: text; /* use text highlight cursor*/
}
class="call-us"
的链接,这涵盖了大多数桌面设备。pointer-events
在所有现代浏览器中都受支持,但IE仅在11+版本中支持。请参见兼容性表。Modernizr.mq
以及Modernizr.touch
来检测屏幕宽度和触摸能力,然后使用jQuery注入电话链接。这个解决方案可以将电话链接保留在源代码之外,只会出现在某个特定宽度以下的触摸设备上(比如说720像素,这可能涵盖大多数手机但排除大多数平板电脑)。// Use event delegation, to catch clicks on links that may be added by javascript at any time.
jQuery(document.documentElement).on('click', '[href^="tel:"]', function(e){
try{
// These user-agents probably support making calls natively.
if( /Android|webOS|iPhone|iPad|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
// Do nothing; This device probably supports making phone calls natively...
} else {
// Extract the phone number.
var phoneNumber = jQuery(this).attr('href').replace('tel:', '').trim();
// Tell the user to call it.
alert("Please call "+phoneNumber);
// Prevent the browser popup about unknown protocol tel:
e.preventDefault();
e.stopPropagation();
}
} catch(e){
console.log("Exception when catching telephone call click!", e);
}
});
tel:
的支持并不取决于屏幕大小、触摸支持、移动设备还是桌面设备等因素。你应该做最简单的事情,始终显示tel:
链接。使用title
解决方案(最佳答案)可以提供一些上下文和/或显示实际号码,这是最清晰的解决方案,而无需检测。 - toastal