2

私はこのhtml行を持っています:

<div class="hide-btn top tip-s" original-title="Close sidebar"></div>

そして、ユーザーがそれをクリックしたときに、テキストを「サイドバーを開く」に変更したいのですが、どうすればよいですか?

これは私のJSです:

$(".hide-btn").click(function(){
    if($("#left").css("width") == "0px"){
        $("#left").animate({width:"230px"}, 500);
        $("#right").animate({marginLeft:"250px"}, 500);
        $("#wrapper, #container").animate({backgroundPosition:"0 0"}, 500);
        $(".hide-btn.top, .hide-btn.center, .hide-btn.bottom").animate({left: "223px"}, 500, function() { $(window).trigger("resize");});
    }
    else{
        $("#left").animate({width:"0px"}, 500);
        $("#right").animate({marginLeft:"20px"}, 500);
        $("#wrapper, #container").animate({backgroundPosition:"-230px 0px"}, 500);
        $(".hide-btn.top, .hide-btn.center, .hide-btn.bottom").animate({left: "-7px"}, 500, function() { $(window).trigger("resize");});
    }
});

お時間をいただきありがとうございます!

4

2 に答える 2

7

jQueryを使用して要素の属性を変更するには、を使用しますattr。jQueryに接続されたイベントハンドラー内では、元のDOM要素はとして使用できますthis。その周りにjQueryラッパーを取得するには、を使用します$(this)。例えば:

$(this).attr("original-title", "new text");

補足:属性は要素original-titleに対して無効です。代わりに属性divの使用を検討してください。data-*

于 2012-04-07T09:53:07.870 に答える
2

私はそのようなことのためにこれをするのが好きです:-)

var a = $(".hide-btn.top");
a.attr('original-title', a.attr('original-title').replace(/Close/, 'Open') );

複雑すぎるように見えるかもしれませんが、後で正確なラベルについて考えを変えることがよくあります。これにより、戻ってすべてを調整する必要がなくなります。

于 2012-04-07T09:55:41.587 に答える