0

私はおおよそこのように見えるhtmlを持っています

<div id="wallPostTemplate">
    <li id='comment-content--oOo-id-oOo-' class='comment-content' comment_id='-oOo-id-oOo-'>
        <a class='comment-avatar' href='-oOo-link-oOo-'>
            <img class='small_avatar' src='<?="../" . MESTO_ZA_UPLOAD_FAJLOVA_KORISNIKA ?>-oOo-korisnikPodaci.Slika-oOo-'></a> 
            -oOo-text-oOo-
    </li>
</div>

ここで、htmlの内容をstingに入れてから、文字列の出現 箇所を、格納したデータに$('#wallPostTemplate').text()動的に置き換えたいと思います。-oOo-id-oOo--oOo-id-oOo--oOo-text-oOo-

今、私はこのことに取り組むために多くの方法を試しましたが、私はいつも解決策がないことになります。これが私の最新の試用版です。ヘルプplsQ_Q

var regex = new RegExp('{\-oOo\-'+index+'\-oOo\-}', "g");
post = post.replace(regex, value);  

また、試してみstr.replaceましたがうまくいきません…新しいラインが問題なようですが、どうやって乗り越えたらいいのかわかりません…。

編集:

理解しやすくするために、jsfidleで例を作成しました。http://jsfiddle.net/DdBFB/ご覧のとおり 何も表示されません。HTMLを介してコンテンツを取得すると機能しません。働き。

4

3 に答える 3

1

私があなたの質問を理解しているなら、あなたの目標はランタイム/クライアント側でロードされる投稿テンプレートのようなものを作ることです。したがって、これが本当にあなたの目標である場合は、jQueryテンプレートプラグインを使用してみることができます

ケースのサンプル:(まだテストされていません)

var tmlpPost = "<div id='${postId}'>" + 
                         "      <li id='comment-content-${postId}' class='comment-content' comment_id='${postId}'>" + 
                         "              <a class='comment-avatar' href='${link}'>" + 
                         "                  img class='small_avatar' src='<?="../" . MESTO_ZA_UPLOAD_FAJLOVA_KORISNIKA ?>${korisnikPodaciSlika}'>" + 
                         "              </a> " + 
                         "              ${postText}" + 
                         "      </li>" + 
                         "</div>";


$.tmpl( tmlpPost, { "postId" : "123" , "link" : "http://posturl.com", "korisnikPodaciSlika" : "something",  "postText" : "Post text goes here..."}).appendTo( "#yourPostWall" );

または使用し<script>ないvarこのフィドルを参照してください

<script id="tmlpPost" type="text/x-jquery-tmpl">
    <div id='${postId}'>
          <li id='comment-content-${postId}' class='comment-content' comment_id='${postId}'>
                  <a class='comment-avatar' href='${link}'>
                      <img class='small_avatar' src='<?="../" . MESTO_ZA_UPLOAD_FAJLOVA_KORISNIKA ?>${korisnikPodaciSlika}'>
                  </a>
                  ${postText}
          </li>
    </div>
</script>

ページの読み込み中:

$("#tmlpPost").tmpl( { "postId" : "123" , "link" : "http://posturl.com", "korisnikPodaciSlika" : "something",  "postText" : "Post text goes here..."}).appendTo( "#output" );​
于 2012-08-22T15:59:16.357 に答える
0

あなたの質問は本当に明確ではありませんが、正規表現に基づいて値を置き換える例を作成しました。

var source = "-oOo-korisnikPodaci.Slika-oOo-";
var id = "korisnikPodaci.Slika";
var regex = new RegExp('\-oOo\-' + id + '\-oOo\-', "gmi");

// Match
source.replace(regex, "hello world"); // => "Hello World"

// Mismatch
source = "-oO!!!o-korisnikPodaci.Slika-oO!!!o-";
source.replace(regex, "hello world"); // => "-oO!!!o-korisnikPodaci.Slika-oO!!!o-"
于 2012-08-22T15:56:06.453 に答える
0

わかりました、答えは正規表現フラグを間違って設定したことだと思ったので、jsfiddleに行ってそれをいじると、そこで答えを見ることができます:)

http://jsfiddle.net/DdBFB/11/

私は複数行のフラグQ_Qを見逃していました....私はphpでさえ正規表現でダメですjsで話さないでください...

于 2012-08-23T05:34:51.893 に答える