我想添加h323:数字样式的链接到HighRise联系号码,以便用户可以单击链接拨打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)我应该使用什么正则表达式来清除href的数字?
发布于 2009-10-01 14:24:48
这应该接近您需要的内容。
$('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的内容以数字开头,它将被包括在匹配中;这种可能性需要考虑吗?
发布于 2009-10-06 12:15:25
这是最终的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);
}
});
}
})();https://stackoverflow.com/questions/1504215
复制相似问题