0

jqueryとjavascriptを使用して、DOM(メモリ内のWebページ)内のすべての単語を置き換える必要があります。私はこれを変換する必要があります:

<html>
<head>
    Hi to all
</head>
<body>
    Hi to all
</body>
</html>

これに:

<html>
    <head>
        Bye to all
    </head>
    <body>
        Bye to all
    </body>
</html>

ただし、タグを変更せずに(そこにある値のみ)

1つの試みは次のとおりです。

jQuery.fn.replaceEachOne = function (objective, rep) {
    return this.each( function(){
                $(this).html( $(this).html().replace( new RegExp('(\\s'+objective+'\\s(?![[\\w\\s?&.\\/;#~%"=-]*>]))', "ig"), rep) );
            }
        );
}

しかし、それはタグ値を置き換え続け、私がそれを置くとページを壊します。

ヘルプ!!!

4

4 に答える 4

1

すべてを新しいテキストに置き換えるのではなく、ヘッダーに他の要素があると推測しています。このようなものを試してみてください

$(function(){
  var element = $('body, head').find('*:contains("Hi to all")');
  element.html('Bye to all');
});
于 2011-01-18T20:58:31.470 に答える
1

タグを変更したくない場合は、テキストだけでなく「html」を変更するのはなぜですか。

jQuery.fn.replaceEachOne = function(objective、rep){
    this.each(function(){を返します
                $(this).text($(this).text()。replace(new RegExp('(\\ s'+ objective +'\\ s(?![[\\ w \\ s?&。\\ / ;#〜% "=-] *>]))'、" ig ")、rep));
            }
        );
}
于 2011-01-18T21:43:10.523 に答える
0

非効率的ではありますが、それを行う1つの方法は次のとおりです。

var element = document.getElementsByTagName('html')[0];
element.innerHTML = element.innerHTML.replace('Hi to all', 'Bye to all');

引用符で囲まれた「Hitoall」は、必要に応じて正規表現に置き換えることもできます。

于 2011-01-18T21:40:09.127 に答える
0

ついに...

みんなありがとう...

jQuery.fn.replaceEachOne = function (objective, reposition) {
    this.contents().each(function(){
        if (this.nodeType == Node.ELEMENT_NODE) {
            $(this).replaceEachOne(objective, reposition);
        } else if (this.nodeType == Node.TEXT_NODE) {
            var label = document.createElement("label");
            label.innerHTML = this.nodeValue.replace(objective, reposition);
            this.parentNode.insertBefore(label, this);
            this.parentNode.removeChild(this);
        }
    }); 
}
于 2011-01-19T15:33:23.770 に答える