2012-04-30 28 views
0

我使用twitter API将推文推送并将其分配给变量。像这样。在文本中查找链接并使用jquery将其包装在标签中

 self.tweets = $.map(data.results, function(tweet){ 
       return{ 
        author: tweet.from_user, 
        url: 'http://twitter.com/' + tweet.from_user + '/status/' + tweet.id_str, 
        image: tweet.profile_image_url, 
        tweet: tweet.text 
       }; 
      }); 

我为我工作。我正在将推文附加到HTML。文字是像这样..

@drwarwick Hi David! Looking forward to catching up with you, but may not get in there today due to work commitments... 

我现在要做的就是将@drwarwick成@drwarwick让我能风格,并相应地联系起来。我如何在推文中找到uisng jquery这样的单词。谢谢。

编辑>>>>>

像上面这样做的东西像stackoverflow。它改变了文字的颜色。

回答

1

我刚刚写了一个辅助方法来做到这一点。

function HighLight(input) 
{ 
    var output=""; 
    var items=input.split(" "); 
    $.each(items,function(index,item){ 
     var word=items[index]; 
     if(word.match("^@")) 
     { 
      word="<span class='highlightSpan'>"+word+"</span>"; 
     }       
     output+=word+" "; 
    });  
    return output; 
} 

工作例如:http://jsfiddle.net/ZSafX/27/

你可以改变你的代码,包括像这样

self.tweets = $.map(data.results, function(tweet){ 
      return{ 
       author: tweet.from_user, 
       url: 'http://twitter.com/' + tweet.from_user + '/status/' + tweet.id_str, 
       image: tweet.profile_image_url, 
       tweet: HighLight(tweet.text) 
      }; 
     }); 
+0

,做帮助。但对我来说,这些推文是通过AJAX加载的。所以这个代码在tweets被加载之前运行。所以当这个函数运行时,DOM中没有任何文本,如果我是对的。 – Subash

+0

@Subash:我相信我们在处理来自ajax请求的请求时添加了突出显示功能调用。所以它应该是很好的去。 – Shyju

相关问题