1

私が取得しているHTMLは、理想的には次のようになります。

<span class="RapidLink1-H">See the more detailed areas of what not</span>

次に、スパンタグをアンカータグに変更することを目的としています。理想的なHTMLを使用して、次のようにしました。

            // Walk through each link tag on this page and replace the content with an actual link
            thisLink.each(function() {

                linkRefTarget = '';

                // Get only the identifier number of the link
                linkId = ($(this).attr('class')).replace(/[A-Za-z$-]/g, '');

                // Match this link with the list of link references
                if (thisLinkRef) {
                    for (var key in thisLinkRef) {
                        if (key == 'link' + linkId) linkRefTarget = thisLinkRef[key];
                    }
                }

                output = '';
                output+= '<a href="#' + linkRefTarget + '" id="link' + linkId + '" class="rapidLink">';
                output+= $(this).html();
                output+= '</a>';

            }).replaceWith(output);

ここで、実際にこの種のHtmlを取得しているときに問題が発生します(Html入力を変更できないことに注意してください)。

<span class="RapidLink1-H">See the</span><span class="RapidLink1-H">more detailed areas of</span><span class="RapidLink1-H">what not</span></span>

質問は:

どうすればそのような壊れたスパンのセットで動作させることができますか?

私は次のことを考えています:

  • 予想されるリンクスパンを見つける
  • すぐ次の要素も同じクラスのスパンであるかどうかを確認します
  • 次に、すぐ次の要素も...であるかどうかを確認します。
  • そしてチェック...
  • 何も見つからない場合は、各スパンのinnerHtmlを1つのアンカータグに結合します

どうすればそのようなループを実現できますか?

ありがとう。

4

1 に答える 1

3

+連続する要素を選択するセレクターがあります:http: //jsfiddle.net/NWWYC/1/

$(".RapidLink1-H + .RapidLink1-H").each(function() {
    // move contents to previous span, remove this span afterwards
    $(this).prev(".RapidLink1-H").append(
        $(this).contents()
    ).end().remove();
});
于 2012-08-17T10:50:18.070 に答える