1

ボタンを押した後にボタンのテキストを変更する簡単なタスクがあります。これが私のスクリプトです:

$(".recent").on("click", 'button', function () {
        $(this).closest($("li")).find($(".content")).slideToggle();
        if ($(this).text="Show content") {
            $(this).text("Hide content");
        } else {
            $(this).text("Show content");
        }
    });

ボタンを押すと、テキストが「コンテンツを非表示」に変わります。しかし、ボタンをもう一度押すと、「コンテンツを表示」に戻りません。簡単なことだと思います...助けていただければ幸いです。

4

4 に答える 4

4

===割り当ての代わりに同等性を使用し=、テキストの代わりに text() を使用する必要があります

$(".recent").on("click", 'button', function () {
        $(this).closest($("li")).find($(".content")).slideToggle();

        if ($(this).text() === "Show content") {
            $(this).text("Hide content");
        } else {
            $(this).text("Show content");
        }
    });
于 2013-03-14T17:46:02.580 に答える
2

===比較に使用します。値を割り当てる which を使用してい=ました。.text()メソッドでもある

$(".recent").on("click", 'button', function () {
    $(this).closest('li').find('.content').slideToggle();
    if ($(this).text() === "Show content") {
        $(this).text("Hide content");
    } else {
        $(this).text("Show content");
    }
});
于 2013-03-14T17:45:55.370 に答える
1

.text()はメソッドであるため、テキストを取得および設定するにはそれを呼び出す必要があります。

$(".recent").on("click", 'button', function() {
    var $this = $(this);

    $this.closest("li").find(".content").slideToggle();

    if ($this.text() === "Show content") {
        $this.text("Hide content");
    } else {
        $this.text("Show content");
    }
});
于 2013-03-14T17:47:30.467 に答える
1

the.textmethodであるため、a が続く必要があります()。また、同等の場合は== not =を使用し、 single=は割り当て用です。

これを試して:

$(".recent").on("click", 'button', function () {
    $(this).closest($("li")).find($(".content")).slideToggle();
    if ($(this).text() == "Show content") {
        $(this).text("Hide content");
    } else {
        $(this).text("Show content");
    }
});
于 2013-03-14T17:47:44.220 に答える