1
(function(){
    if ($('#right-col').hasClass('shown')){
        $(this).swipe({
            swipe:function(event, direction, distance, duration, fingerCount) {
                $(this).addClass('hidden');//.text(direction);
            }
        });
    }; 
})();

これは、私が取り組んでいる Web プロジェクトのスワイプ機能です。しかし、どういうわけか、私のコードは if ステートメントでは機能しません。これだけを使用する場合:

$('#right-col').swipe({
  swipe:function(event, direction, distance, duration, fingerCount) {
    $(this).addClass('bg-blue').text("You swiped " + direction );
  }
})

それは正常に動作します。上記の自己呼び出し関数で特に何が問題なのか、誰か教えてもらえますか?

4

2 に答える 2

1

すると$(this).swipe(...)thisですwindow。ID を使用して要素を選択します。

(function () {
    if ($('#right-col').hasClass('shown')) {
        $('#right-col').swipe({
            swipe: function (event, direction, distance, duration, fingerCount) {
                $(this).addClass('hidden'); //.text(direction);
            }
        });
    };
})();

#right-col要素のクラスが後で変更されても、スワイプ機能は影響を受けないことに注意してください。

于 2013-10-31T15:39:12.980 に答える