如何从不同的函数手动传递“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");
我在这里使用了事件名称间距来区分普通的窗口滚动和您专门的点击滚动。