2009-10-01 53 views
0

我要添加H323:編號樣式鏈接高層聯絡號碼,以便用戶可以點擊一個鏈接來撥打IP電話......高層jQuery的解析

我在看的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開頭的號碼,去掉空格,堅持在內部跨度的鏈接有像

h323:4411231231312 

一個href即剝離括號中的0。

任何幫助將被以下任何一種很好的接收。

(1)如何匹配包含+ \ d \ d數字的td? (2)我如何使用選擇器來排除跨度,當我從td得到數字時(3)我應該用什麼正則表達式來清理href的數字?

+0

時,它的後面1-3個數字,可悲... – Dycey 2009-10-01 14:20:38

回答

2

這應該是接近你所需要的。

$('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的內容與將被列入比賽以數字開頭;這是否可能需要考慮?

+0

感謝。我添加了一個包含最終GreaseMonkey腳本的答案... – Dycey 2009-10-06 12:14:14

1

下面是最終的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); 
     } 
    }); 

} 

})();