我通过getJSON拉动推文并将它们写入Google地图infowindow与JavaScript。问题是,这些推文带有文本链接,但没有格式(没有ID /类/任何缩小查找和替换的范围)。这是我现在使用查找文本代码捣成泥,但我不能把它包裹不管它发现在<a>
标签正确显示的链接:用jQuery包装<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);
难道我忽略的东西或有谁知道更好的方法来做到这一点?
如果你不想使用jQuery这个工作得很好:https://gist.github.com/ryansmith94/0fb9f6042c1e0af0d74f – 2017-10-10 19:28:24