0

クリック時にコールバック関数を追加しようとしています(ユーザーがボタンを2回クリックしたとき)。

私は持っています

$('#toggle').live('click', function(){
        $this=$(this);
        $this.attr('id', 'detail')
        turn()
    }, function(){
        $this.attr('id','detail_close')
        turn()
    })

idをdetail_closeに変更して、2回目のクリック後にturn()を再度呼び出すことができないようです。任意のヒント?どうもありがとう!

4

2 に答える 2

1
   var count = 0; // a counter
   $(document).on('click', '#toggle', function(){

        $this = $(this);
        $this.prop('id', 'detail');  // may use prop(), instead of attr()

        count++;  // increase counter value

        // checking for counter value
        if(count > 1) {
             // do something on second click

             $this.prop('id', 'detail_close'); // change id
             turn();  // call turn()

             count = 0; // reset the counter
        }
    });

.live()廃止されたため、デリゲート イベント処理を行うには、次のようなメソッドを使用します.on()

$(StaticParent).on(eventName, target, handlerFunction)
于 2012-09-12T18:12:42.983 に答える
0
$(document).on('click', '#toggle', function(){
    $this=$(this);
    if($this.prop('id') == 'detail'){
        $this.prop('id', 'detail_close');
        turn();
    }else{
        $this.prop('id', 'detail');
        turn();
    }
});

最新バージョンのjQueryでは.onと.propを使用しています。

于 2012-09-12T18:09:52.553 に答える