How to disable link to phone number when on Desktop?

后端 未结 20 949
清酒与你
清酒与你 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:57

    This way works without adding any more CSS.

    Try replacing the a tag with something else like a span tag, but only for mobile browsers of course. Benefits are that you are not cloaking this a with CSS preventing default action while keeping it still as a link. It won't be a anymore, so you won't have to worry.

    I've created an example to here. Bold phone there works this way, see code below.

    I took piece of code from one of the respondent to define if browser is mobile. And you have to mark these links with class="tel" or find a way to determine if the href has "tel:" in it. JQuery is also required.

    // define if browser is mobile, usually I use Anthony's Hand mobile detection library, but here is simple detection for clarity
    var probablyPhone = ((/iphone|android|ie|blackberry|fennec/).test(navigator.userAgent.toLowerCase()) && 'ontouchstart' in document.documentElement);
    if ( !probablyPhone ) {
        // find all the A with "tel:" in it, by class name
        $('a.tel').each(function(){
            var span = document.createElement('span');
    
            // SPAN inherits properties of A, you can also add STYLE or TITLE or whatever
            span.innerHTML = this.innerHTML;
            span.className = this.className;
    
            // replace A with SPAN
            this.parentNode.insertBefore(span, this);
            this.parentNode.removeChild(this);
        });
    }
    

提交回复
热议问题