1

ここに私の $.ajax 関数があります:

$("div#eventBox").click(function () {
    $.ajax({
        url: "/AJAX Pages/Test.cshtml",
        async: true,
        type: "GET",
        success: function (response) {
            $("div#eventBox").html($(response).find("#bodyP").text());
        },
        error: function (jqXHR, textStatus, error) {
            alert("The AJAX function didn't work.");
        }
    });
});

問題はここにあります(上記のスニペットから取得):

$("div#eventBox").html($(response).find("#bodyP").text());

しかし、ここにある解決策によると:

ajax レスポンスの特定の部分を取得して変数に入れる

そしてここ;

jQuery の .post() メソッドで HTML ページの一部だけを選択しますか?

私のコードはうまくいくはずです。

JavaScript コンソールに表示されるエラーは次のとおりです (これにより、ターゲットの ajax ページの html も明らかになります)。

Uncaught Error: Syntax error, unrecognized expression: <div id="eventPageWrapper" style="margin: auto; text-align: center; width: 100%;">
    <span style="text-align: center; margin: 5px 10px;">Harlem Ambassadors-Basketball Fundraiser for American Red Cross</span></br>
    <p id="bodyP" style="text-align: center; margin: 5px 10px;">
        The Harlem Ambassadors Basketball team will be in Okmulgee today for a fundraiser for the American Red Cross. It will be held at the Brock Gymnasium and starts at 4:00 p.m. Tickets are on sale now and can be purchased through the American Red Cross. Advanced tickets are $8.00 per student and $10.00 per adult. If the tickets are purchased at the door, they are $10.00 per student and $12.00 per adult. Contact the American Red Cross for tickets or more information at (918)-756-0966 or (918)-932-7323.
    </p>
</div> 

これはおそらく簡単な解決策ですが、何が間違っているのかわかりません。

アップデート

空白の原因を見つけ、単純な HTML コメントを使用して簡単に削除できました。

@{
    Layout = "";
}<!--  HERE WAS WHERE ALL THE WHITE SPACE WAS COMING FROM BEFORE I ADDED THIS COMMENT
--><div id="eventPageWrapper" style="margin: auto; text-align: center; width: 100%;">
    <span style="text-align: center; margin: 5px 10px;">Harlem Ambassadors-Basketball Fundraiser for American Red Cross</span></br>
    <p id="bodyP" style="text-align: center; margin: 5px 10px;">
        The Harlem Ambassadors Basketball team will be in Okmulgee today for a fundraiser for the American Red Cross. It will be held at the Brock Gymnasium and starts at 4:00 p.m. Tickets are on sale now and can be purchased through the American Red Cross. Advanced tickets are $8.00 per student and $10.00 per adult. If the tickets are purchased at the door, they are $10.00 per student and $12.00 per adult. Contact the American Red Cross for tickets or more information at (918)-756-0966 or (918)-932-7323.
    </p>
</div>
4

1 に答える 1

2

代わりに次のようにします。

$("#eventBox").html( $("<div>").html(response).find("#bodyP").text() );

この$()メソッドは、で始まらない html 文字列を受け入れることはできません<

eventBox セレクターも修正しました。

于 2013-05-17T15:50:08.120 に答える