0

jQueryで2つのことが必要です(twitterのような)

  1. 通常のリンクを正規表現する
  2. リンクが画像かウェブサイト/ドキュメント/などかどうかを確認してください(ライトボックスなどを使用できます)。

たとえば、json オブジェクトから次のテキストがあります。

こんにちは、私はこの http://flicker.com/imageを作成し、 http://www.google.com #home #aloneにアクセスしました

  1. コンテンツが画像の場合、リンクを確認する必要があります
  2. A 要素を追加する必要があります。
  3. ハッシュタグ「#」もリンクする必要があります。

私はjQueryについて少し知識があり、次のスクリプトを編集/作成しました(おそらく良くないかもしれませんが、現時点では機能しています)

var username='yourname'; // set user name
var format='json'; // set format, you really don't have an option on this one
var url='http://api.twitter.com/1/statuses/user_timeline/'+username+'.'+format+'?callback=?'; // make the url
var count = 0; // Start from 0
var maxTweet = 10; // Maximum tweets (max limit is 15)
$.getJSON(url,function(tweet){ // get the tweets
    $.each( tweet, function(i, l){
        var month1 = {}; // building my month array
        month1['Jan'] = '01';
        month1['Feb'] = '02';
        month1['Mar'] = '03';
        month1['Apr'] = '04';
        month1['May'] = '05';
        month1['Jun'] = '06';
        month1['Jul'] = '07';
        month1['Aug'] = '08';
        month1['Sep'] = '09';
        month1['Oct'] = '10';
        month1['Nov'] = '11';
        month1['Dec'] = '12';
        var d = tweet[count].created_at; // getting date
        var d = d.split(" "); // Splitting date
        var e = tweet[count].created_at.split(" "); // Splitting date
        var a = d[1]; // Month
        var month = month1[a]; // still month
        var d = d[5] + '-' + month + '-' + d[2] + 'T' + d[3] + 'Z' + d[4]; // date like 30-05-2011T13:45:45Z+1000
        var date = prettyDate(d); // using prettyDate founded on the Web
        if(count < maxTweet){
        if(date = 'undefined'){
            var date = e[2] + '-' + month + '-' + e[5] + ' om ' + e[3];
        } // if it is out of range
    $("#twitter .slides_container").append('<p> <a href="http://twitter.com/#!/' + tweet[count].user.name + '" target="_blank"><img src="' + tweet[count].user.profile_image_url + '" alt="' + tweet[count].user.name + '" class="twitpic"></a> ' + tweet[count].text + '<br /><em>' + date + '</em></p>')
}
    });
count++;
});

tweet[count].text = 正規表現が必要な場所

編集済み (count++;)

4

1 に答える 1

0

あなたのつぶやきをリンクするために、このようなものが便利だと思うかもしれません。

html = tweet[count].text
    .replace(/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@@#\/%?=~_|!:,.;]*[-A-Z0-9+&@@#\/%=~_|])/ig, '<a href="$1" target="_blank" rel="nofollow">$1</a>')
    .replace(/(?:^| )[@]+(\w+)/ig, ' @<a href="http://www.twitter.com/$1" class="user" target="_blank" rel="nofollow">$1</a>')
    .replace(/(?:^| )[#]+([\w\u00c0-\u00d6\u00d8-\u00f6\u00f8-\u00ff\u0600-\u06ff]+)/ig, ' <a href="http://search.twitter.com/search?q=&tag=$1&lang=all" class="hash" target="_blank" rel="nofollow">#$1</a>');

残念ながら、リンクに .jpg が含まれているかどうかなどの仮定をしない限り、URL の最後に何があるかは、そこに行かないとわからないことがあります (これは、ライトボックス機能を実装するために必要なことです)。

于 2012-05-24T11:16:27.567 に答える