0

ここで関数を作成しました:

function locationOfVideo(){//Outputs video location div according to post text
    var ytVideoDiv = '<div class="youtube-video"><div id="<?php echo $divName; ?>-video"></div></div>';

    $j('#main .content:contains("<--video-->")').replace('<--video-->', ytVideoDiv);
}

これでうまくいくと思っていましたが、ひどく間違っていて、少し疲れています。とにかく、ここで何が間違っているのですか?

4

4 に答える 4

3

replaceは、jQueryオブジェクトのコンテンツを置き換えるのではなく、文字列などの一部を置き換えるプレーンなjs関数です。

選択した要素のすべてのコンテンツを置き換えることが目標であり、適切なセレクターを使用している場合は、次を使用html()して実行できます。

$j('#main .content:contains("<--video-->")').html(ytVideoDiv);

または文字列だけを置き換える<--video-->

$j('#main .content:contains("<--video-->")').html(function(i,html) {
    return html.replace('&lt;--video--&gt;', ytVideoDiv)
});​

フィドル

于 2012-08-17T03:02:23.897 に答える
1
$j('#main .content:contains("<!--video-->")').replace('<--video-->', ytVideoDiv);

する必要があります:

var videoNode = $j('#main .content:contains("<!--video-->")');
videoNode.html(videonode.html().replace('<!--video-->', ytVideoDiv));

また:

$j('#main .content:contains("<!--video-->")').html(function(index, oldhtml) { return oldhtml.replace('<--video-->', ytVideoDiv); });
于 2012-08-17T03:04:32.737 に答える
1

<--video-->html で使用しないでください{video}

次に、次のようにします。

$('#main .content:contains("{video}")').replaceWith(function() {
  return $(this).text().replace('{video}', ytVideoDiv);
});
于 2012-08-17T03:14:37.457 に答える
0

abbrのような特別なタグを付けて要素を出力することをお勧めします。jquery-timeagoのようなjavascriptテンプレートusabeをチェックしてください。

<abbr class="timeago" title="2011-12-17T09:24:17Z">December 17, 2011</abbr>

交換:

$('abbr.timeago').timeago()
于 2012-08-17T03:28:19.690 に答える