2

私は DOM を持っており、この DOM に Web ページの HTML 全体をロードしました。スタイルタグ内であっても、フォント関連のタグをすべて削除したい。

4

2 に答える 2

4

指定されたタグのセットのいずれかを に変換する純粋な Javascript 関数を次に示します<span>

function stripFonts(el, tags) {
    if (el.tagName && el.tagName in tags) {

        // replace the element with a span
        var old = el, el = document.createElement('span');
        old.parentNode.replaceChild(el, old);

        // and move the children
        while (old.hasChildNodes()) {
            el.appendChild(old.firstChild);
        }
    }

    // recursive test all of this node's children
    var child = el.firstChild;
    while (child) {
        child.removeAttribute('style');  // NB: removes *all* style attributes
        stripFonts(child, tags);
        child = child.nextSibling;
    }
}

var tags = { 'B': 1, 'FONT': 1, 'STRIKE': 1 };

http://jsfiddle.net/alnitak/4fBRQ/のデモ

を使用する<span>と、元の DOM ツリーが維持されるためコードが簡素化され、隣接するテキスト ノードを一緒に折りたたむ必要がなくなります。

削除するタグを追加するのは簡単です。ここに示すように、すべての属性を削除するのは簡単ですが、フォント固有のインライン スタイル タグを処理するには追加の作業必要です。 style

于 2012-07-16T13:26:59.217 に答える
2
     $("font").attr("face", "");
    $("*").css("font", "");

jQuery を使用して、DOM 内の font 要素のファクト属性とすべての css をクリアすることができます。

于 2012-07-16T13:26:39.323 に答える