我想向 HighRise 联系号码添加 h323:number 样式链接,以便用户可以单击链接拨打 IP 电话...

我正在查看的 html 是:

<table>
  <tbody>
    <tr>
      <th>Phone</th>
      <td>+44 (0)1123 1231312<span>Work</span></td>
    </tr>
    <tr>
      <th></th>
    <td>+44 (0)777 2342342<span>Other</span></td>
    </tr>
  </tbody>
</table>

基本上我想取出 td 中以 +44 开头的数字,去掉空格并粘贴在跨度内的链接中,该链接具有类似 href 的内容

h323:4411231231312  

IE。就是去掉括号里的0。

对于以下任何一项,我们将不胜感激。

(1)如何匹配包含+\d\d数字的td?(2)当我从TD获得数字时,如何使用选择器来排除跨度(3)我应该使用什么正则拨号来清理HREF的数字?

有帮助吗?

解决方案

这应该接近您所需要的。

$('tbody td').each(function() {
    // match a sequence of digits, parentheses and spaces
    var matches = $(this).text().match(/[ \d()]+/);

    if (matches) {
        // remove the spaces and stuff between parentheses
        var href = 'h323:' + matches[0].replace(/\s|\(.*?\)/g, '');
        var link = $('<a/>').attr('href', href);

        $('span', this).append(link);
    }
});

不过需要注意的是,如果 span的内容以数字开头,它将包含在匹配中;这种可能性需要考虑吗?

其他提示

这是最终的 GreaseMonkey 脚本 - 可能对某人有用......

// ==UserScript==
// @name          HighRise Dialler
// @namespace     
// @description   Adds a CALL link to HighRise Contacts.
// @include       https://*.highrisehq.com/*
// @require       http://code.jquery.com/jquery-latest.min.js
// ==/UserScript==

(function(){

GM_xmlhttpRequest({
   method: "GET",
   url: "http://jqueryjs.googlecode.com/files/jquery-1.2.6.pack.js",
   onload: run
});

function run(details) {

   if (details.status != 200) {
       GM_log("no jQuery found!");
       return;
   }

   eval(details.responseText);
   var $ = jQuery;

   //do something useful here....

   $('table td').each(function() {
       var matches = $(this).text().match(/^\+*?[\d\(\) ]+/);

       if (matches) {
         var href = 'h323:' + matches[0].replace(/\+44|\+|\s|\(|\)/g, '');
         var link = $(' <a>CALL<a/>').attr('href', href);
         $(this).find('span').append(link);
       }
   });

}

})();
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top