开发者

I want to loop every text in a div .each(function() - jquery

I have these URLs from a text file

"http://xxxxxxxxxxxxxx.xom.sssss/data/feed/base/user/592591?kind\u003doium\u00flt\u065drss\u002s\u0asn_7S\udess\aawac", "url": 

"http://xxxxxxxxxxxxxx.xom.sssss/data/feed/base/user/592591?kind\u003doium\u00flt\u065drss\u002s\u0asn_7S\udess\aawac", "url":  }, 

"http://xxxxxxxxxxxxxx.xom.sssss/data/feed/base/user/592591?kind\u003doium\u00flt\u065drss\u002s\u0asn_7S\udess\aawac", "url": 

I want to do a loop, in #from and append it to #here

$('#from').match(/http:\/\/\S+/).each(function(){;
    var t = $(this).text();
    $(t).appendTo('#here');
});

There seems to be an error .

HTML CODE

<div id="from">

<? 
$fl = "file.txt"; 
$file1 = fopen($fl, "r");
$content = file_get_contents($fl); 
echo $content;       
?>

&l开发者_StackOverflow中文版t;/div>

<div id="here"></div>

I could easily use PHP, but I want it to make it in jQuery, since it gives me the freedom to pull any http://, be it anywhere in the text file or how complicated it may be.

Thanks Jean


If you just want what look like links from the text then do something like:

var text = $("#form").text();
var r = /\b(http://.*?)(?=\s|\z)/;
var here = $("#where");
while (m = r.exec(text)) {
  $("<span>").text(m[1]).appendTo("#here");
}

Your question is pretty vague about what you're dealing with and what you want the end result to be so I've made a lot of assumptions:

  • This won't capture URLs in images or anchors.
  • I don't know what the destination is or how (or even if) you want to wrap the URLs. In my example I'm just putting each in a <span>;
  • I've been fairly liberal when it comes to interpreting what a URL is. I've assumed anything that starts on a word boundary with "http://" is a URL up to the end of the text or a white space character, whichever comes first. You can make this stricter if you wish.


You said:

I am trying to get all http links from #from and appending it to #here.

If I undestood you correctly, you need something like this:

$('#from a').appendTo('#here');

This will take all links (<a>-elements) from #from and move them to #here. Is this what you wanted?

If you don't want to move the links, but only copy them, you can use .clone():

$('#from a').clone().appendTo('#here');


$("#from a[href^='http']").appendTo('#here')
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜