How to disable link to phone number when on Desktop?

后端 未结 20 952
清酒与你
清酒与你 2021-02-03 23:47

How can I add a phone number to a website that is clickable but hides the link when I\'m browsing on a website that doesn\'t support touch.

I could use Modernizr to set

20条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-03 23:58

    I've had success with this using Modernizr, specifically the touch test. It's not a perfect solution in that it doesn't do anything to help tablets or touch-enabled laptops, but it works in most desktop browsing situations.

    HTML:

    Call us at: 1-800-BOLOGNA

    CSS:

    .no-touch a.call-us {
      color: black;            /* use default text color */
      pointer-events: none;    /* prevents click event */
      cursor: text;            /* use text highlight cursor*/
    }
    

    The above CSS targets links with class="call-us" on non-touch devices which covers the majority of desktops.

    Note that pointer-events is supported in all modern browsers but IE only supports it in versions 11+. See the compatibility chart.

    Another solution, still imperfect, would be to use Modernizr.mq along with Modernizr.touch to detect screen width and touch capability and then inject the phone link with jQuery. This solution keeps the phone link out of the source code and then only appears on touch devices smaller than a certain width (say 720px which will probably cover most phones but exclude most tablets).

    Ultimately, it's up to the browser vendors to come up with a bulletproof solution here.

提交回复
热议问题