我的应用程序从instagram中提取图片,我想知道如何使用JS在文档中循环,简单地查找每个带有#标签的项目,并在其上附加一个URL。
即
My instagram photo contains the following tags #instagram #photo #tags结果看起来像这样
我的instagram照片包含以下标签#instagram #photo #tags
发布于 2012-07-03 22:07:52
尝尝这个
var x = "My instagram photo contains the following tags #instagram #photo #tags";
var split = x.split(" ");
for(var i=0; i< split.length; i++){
if(split[i].indexOf('#') >= 0){
split[i] = '<a href=\"http://myapp.com/tag/'+split[i].substring(1)+'\">'+split[i]+'</a>';
}
}
console.log(split.join(" "));这是fiddle http://jsfiddle.net/9XQBC/
发布于 2012-07-03 22:01:18
你可以在href后面加上一个#,试试这个
var tag = '#instagram';
$('.myPhoto').attr('href',tag);//this will change current href发布于 2012-07-03 22:01:47
找到了一个简单的解决方案
hashtag_regexp = /#([a-zA-Z0-9]+)/g;
function linkHashtags(text) {
return text.replace(
hashtag_regexp,
'<a class="hashtag" href="/search?q=$1">#$1</a>'
);
}
$(document).ready(function(){
$('.comment').each(function() {
$(this).html(linkHashtags($(this).html()));
});
});https://stackoverflow.com/questions/11312481
复制相似问题