0

別の関数から「this」値を手動で渡す方法

jQuery

$('#divName ul li a').click(function() {
    $(this).addClass('active');
});
$(window).scroll(function() {
    //for condition a
    //the value of this will a

    //for condition b
    //the value of this will be
});​
4

2 に答える 2

1
  var elem = '';
$('#divName ul li a').on('click',function(e){
     $(this).addClass('active');
     elem = $(this);
     alert(elem);
    e.preventDefault();
});


$(window).scroll(function(){

  // Use the elem variable here
    console.log('The Variable this is recently cliked element : '+ elem)   
 //for condition a
 //the value of this will a

//for condition b
 //the value of this will be
});​

このフィドルをチェックしてください

于 2012-09-19T21:33:04.390 に答える
0

クリックとスクロール機能を実装しようとしていますか? クリック ハンドラーにスクロール ハンドラーを配置します。

$("div").click(function(e) {
    var target = this;
    $(window).on("scroll.clickScroll", function(e) {
        // Use "target" as the reference to what was clicked on.
    }
}

次のようにクリックが停止したら、ハンドラーを削除する必要があることに注意してください。

$(window).off("scroll.clickScroll");

ここでは、通常のウィンドウ スクロールと特殊なクリック スクロールを区別するために、イベント名の間隔を使用しています。

于 2012-09-19T22:51:45.570 に答える