0

テキストの単一のスニペットを選択し、それを H1 でラップする方法を見つけようとしています。サイトの 1 か所でしか使用されていないため、本当に軽量なものを探しています。それは非常に単純に思えますが、答えが長すぎて気にすることができないか、うまくいかないようです。

HTML

<div class="innerWrapper">
  Office Current Listings
</div>

JS

$('div.innerWrapper:contains("Office Current Listings")').html()
.replace('Office Current Listings', '<h1>Office Current Listings</h1>');

この前の質問の回答を使用しましたが、うまくいかないようです。デモを用意しましたので、よろしければご覧ください。

http://jsfiddle.net/gjtjh/

4

2 に答える 2

4

これはどう?

$('div.innerWrapper:contains("Office Current Listings")').wrapInner('<h1/>');​​​​

- アップデート -

サブ div を含む任意の場所にあるテキストを処理するには、これを試してください。

$('div.innerWrapper').each(
    function(index) {
        var $el = $(this);
        var html = $el.html();
        $el.html(html.replace(/Office Current Listings/g, "<h1>Office Current Listings</h1>"));
    }
);​

http://jsfiddle.net/eDyBu/3/

于 2012-12-10T20:27:55.030 に答える
2

http://jsfiddle.net/gjtjh/1/

var div = $('div.innerWrapper:contains("Office Current Listings")');
var html = div.html();
div.html(html.replace('Office Current Listings', '<h1>Office Current Listings</h1>'));​

実際には、div の html を設定していません。 replaceの関数でstringあるため、テキストを置き換えてから何もしません。

于 2012-12-10T20:21:09.497 に答える