1

約12段落の長さのテキスト本文があります...テキスト内には、ドキュメント全体(3段落ごと)で自動的に強調表示して再配置し、その後ブロック引用を削除してテキストが表示されるようにしたいブロック引用がいくつかあります二回。1 回目は強調表示されたスニペットとして自動的に並べられた位置に、2 回目はブロックなしの元の場所に...

ある程度は機能しますが、順序に従っていないため、何かが欠けているように感じます. (3,6,9 など) だと思いますが、何かに振り回されているように見えますか?

jQuery ->
    content = $('article.city-review div')
    content.find('blockquote').each (index) ->
        line_space = (index+1)*3
        quote_tag = '<span class=\"quote_left\">'+$(this).text()+'</span>'
        content.find('p:nth-child('+line_space+')').prepend(quote_tag)
        $(this).contents().unwrap().wrap('<p></p>')

アップデート:

入力は次のようになります。

<p>Text</p>
<p>More Text</p>
<p>Text</p>
<p>More Text</p>
<blockquote>Text</blockquote>
<p>Text</p>
<p>More Text</p>
<blockquote><p>Sometimes these appear</p></blockquote>

出力には、空の p タグ<p></p>とネストされた p タグが表示されます<p><p>Something</p></p>

4

1 に答える 1

2
jQuery ->
    content = $('article.city-review div')
    content.find('blockquote').each (index) ->
        position = [2,6,10]
        line_space = position[index]
        text = $(this).text()
        quote_tag = '<span class=\"quote_left\">'+text+'</span>'
        content.find('p:nth-child('+line_space+')').after(quote_tag)
        $(this).replaceWith('<p>'+text+'</p>')

配列を作成することは、間隔をテストする簡単な方法であり、無制限の長さが必要でない限り機能します。アンラップを削除して を使用すると、別のタグ内に新しい引用符が必要でない限り、代わりにreplacewith簡単に使用できます。afterprepend

于 2012-11-20T15:25:31.377 に答える