How to disable link to phone number when on Desktop?

后端 未结 20 1007
清酒与你
清酒与你 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-04 00:07

    For me the easiest, yet simplest method without any new classes / setup is via 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 just goes like this:

    (+12)3 456 7
    

    This works for modern browsers & IE 11+. If you need to include 8 < IE < 11 add the following to your javascript, since pointer-events dont work in IE:

    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');
        }
    }
    

    EDIT: i found another answer on another thread, that may be useful for you - SO - Answer

提交回复
热议问题