我正在通过getJSON拉取推文,并使用JavaScript将它们写入Google地图信息窗口。问题是,这些推文带有文本链接但没有格式(也没有id / class / 任何可用于缩小查找范围的内容)。这是我目前使用的代码混搭,用于查找文本,但我无法使其在<a>
标记中包装它找到的任何内容以正确显示链接:
function wrap( str ) {
return '<a href="' + str + '">' + str + '<\/a>';
};
function replaceText() {
var jthis = $(this);
$("*").each(function () {
if (jthis.children().length == 0) {
jthis.text(jthis.text().replace(/\bhttp[^ ]+/i, wrap));
}
});
}
$(document).ready(replaceText);
$("html").ajaxStop(replaceText);
我是否忽略了什么,或者有没有更好的方法来做这件事?