代码之家  ›  专栏  ›  技术社区  ›  Dycey

高层jquery解析

  •  0
  • Dycey  · 技术社区  · 15 年前

    我想添加H323:数字风格的链接到高层联系人号码,以便用户可以单击链接拨打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  
    

    即去掉括号中的0。

    任何帮助都将很好地接受以下任何一项。

    (1)我如何匹配包含+\d\d数字的TD? (2)当我从td中获取数字时,如何使用选择器排除跨度? (3)我应该使用什么regex来清除href的编号?

    2 回复  |  直到 15 年前
        1
  •  2
  •   Alex Barrett    15 年前

    $('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

        2
  •  1
  •   Dycey    15 年前

    // ==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);
           }
       });
    
    }
    
    })();