【问题标题】:Is there an elegant way to make phone numbers clickable on reactive websites?有没有一种优雅的方法可以在响应式网站上点击电话号码?
【发布时间】:2019-01-26 03:22:32
【问题描述】:

我使用了一些 CSS 来检测视口大小,并在用户使用电话时将电话号码显示为可点击的链接。虽然效果很好,但对我来说似乎很笨重。

CSS:

@media screen and (min-width: 0px) and (max-width: 400px) {
  #show-on-mobile { display: block; }  /* show it on small screens */
  #hide-on-mobile { display: none; } 
}

@media screen and (min-width: 401px) and (max-width: 2024px) {
  #show-on-mobile  { display: none; }   /* hide it elsewhere */
  #hide-on-mobile  { display: block; }

}

HTML

<div id="show-on-mobile">
    <a href="tel:+14109842714">+1 000-000-0000</a>
</div>
<div x-ms-format-detection="none" id="hide-on-mobile">
    +1 000-000-0000
</div>

有没有更好、更精简的解决方案?

【问题讨论】:

标签: html css


【解决方案1】:

如果您不介意使用一点 javascript,您可以将电话号码留在 &lt;a&gt; 标记中,如果窗口宽度大于 400 像素,则删除 href 组件。

HTML:

<a href="tel:+14109842714">+1 000-000-0000</a>

JavaScript:

window.onload = function(){
    if ( window.innerWidth > 400 ) {
        document.querySelector('[href="tel:+14109842714"]').href = '';
    }
};

CSSTricks 有一篇很棒的小文章:https://css-tricks.com/how-to-disable-links/

【讨论】:

    猜你喜欢
    • 2014-09-06
    • 1970-01-01
    • 2012-08-07
    • 1970-01-01
    • 2011-06-02
    • 1970-01-01
    • 2021-08-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多