別の関数から「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
});
別の関数から「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
});
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
});
このフィドルをチェックしてください
クリックとスクロール機能を実装しようとしていますか? クリック ハンドラー内にスクロール ハンドラーを配置します。
$("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");
ここでは、通常のウィンドウ スクロールと特殊なクリック スクロールを区別するために、イベント名の間隔を使用しています。