3

これに対してさまざまなアプローチを試みましたが、何も機能していません。今私は持っています:

    $(document).ready(function () {
    $(".page").click(function () {

        var html = $(document).html();

        html.replace("[", "<");

        alert("here");
    });


 });

これは機能しません。また、たとえばJqueryの例がどのように機能するかは何もできません

  $("[").replaceWith("<");

多くの例がクエリの一部としてそれを持っているように見えますが、.replace は jQuery にもないように思えます。助言がありますか?これは私をイライラさせ始めています。特定の div を渡そうとしましたが、まだ機能しません。何か案は?どんな助けでも大歓迎です!

4

3 に答える 3

5

これは、あなたの望むことですか?

$(document.documentElement).html(function(i,val){
    return val.replace(/\[/g,'<');
});

ドキュメント自体は ではないため、代わりにdocument.documentElementDOM Elementを使用する必要があります。

于 2012-07-23T20:33:38.963 に答える
0

ラインhtml.replace("[", "<");は何もしていません

htmlは文字列であるため、文字列を置き換え、出力には何もしません。

var foo = html.replace(/\[/g, "<");
$(document).html(foo);
于 2012-07-23T20:48:21.617 に答える
0

HTMLから変数を作成しているが、それをページに送り返していないようです:

$(document).ready(function () {
    //EDIT:
    // $(".page").click(function () { changed to a bind type event handler to
    // prevent loss of interactivity. .on() requires jQuery 1.7+ I believe.
    $(".page").on('click', function () {   
           var htmlString = $('body').html();

            // To replace ALL occurrences you probably want to use a regular expression
            // htmlString.replace(/\[/g, "<");
            htmlString.replace("[", "<");


            //Validate the string is being manipulated
            alert(htmlString);

            // Overwrite the original html
            $('body').html(htmlString);
    });
});

私はこれをテストしていないことに注意してください。しかし、私が間違っていなければ、jQuery の .html() メソッドは get/set に使用され、必ずしも直接操作を行うわけではありません。

于 2012-07-23T20:47:05.507 に答える