jQuery find phone numbers on page and wrap in <a href=“tel: ”> links

▼魔方 西西 提交于 2019-12-05 13:56:47

Using John's answer as a base I got it to work with:

var regex = /((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}/g; 

var text = $("body:first").html();

text = text.replace(regex, "<a href=\"tel:$&\">$&</a>");

$("body:first").html(text);

Demo: http://jsfiddle.net/PShYy/2/

You can replace all instances of your phone number with regex

js

    var text = $("body:first").html();

    text = text.replace(/(phone\-number)/g, "<a href=\"tel:$1\">$1</a>");

    $("body:first").html(text);

html

        <div id="departments">
           <ul>
             <li> Department 1 - phone-number</li>
             <li> Department 2 - phone-number</li>
             <li> Department 3 - phone-number</li>
           </ul>
        </div>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!