سؤال

أريد أن أضيف h323:عدد وصلات إلى ناطحات السحاب أرقام الاتصال ، بحيث يمكن للمستخدمين النقر على الرابط لطلب الهاتف الملكية الفكرية...

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, قطاع من المساحات عصا في رابط داخل span أن href مثل

h323:4411231231312  

أيهو تجريد من 0 بين قوسين.

أي مساعدة ستكون greatfully الواردة على أي من التالية.

(1) كيف يمكنني مباراة td تحتوي على +\d\d الأرقام ؟ (2) كيف يمكنني استخدام محددات استبعاد span عندما أحصل على عدد من td (3) ما regex يجب استخدامها لتنظيف رقم 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's المحتوى يبدأ برقم سيتم تضمينه في المباراة ؛ هذا احتمال أن يحتاج إلى أن بلغت ؟

نصائح أخرى

وهنا النهائي سكريبت - قد تكون مفيدة لشخص ما...

// ==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