开发者

How do I get this JQuery to target & shorten URL's in a string?

I'm just starting out wi开发者_如何学Pythonth JQuery.

My Question: I want to know if I can modify this code below to shorten long URLs, but ignore anything that isn't a URL?

My Problem: This code fragment targets everything in the DIV ('tasks-overflow'). Although I just need it to affect URL's in that DIV based on protocol(http://...). There are sentences on either side of the URL.

$(document).ready(function(){
  $(".tasks-overflow:contains('http://')").each(function(){
    $(this).text("http://...");
  });
});

Any help would greatly be appreciated, thanks


Changed my awnser after I read your question better. Try this:

$(document).ready(function(){
  $(".tasks-overflow:contains('http://')").each(function(){
    var self = $(this)
    , txt = self.text();
    txt = txt.replace(/(http[s]*:[\/\\]{2})[^\s]*/g,'$1...');
    self.text(txt);
  });
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜