简体   繁体   English

指定特定类别的onclick

[英]target specific class onclick

Hi I have 2 classes which have an on click event on it, How can I target the event on click that would not affect the other class. 嗨,我有2个类别都带有点击事件,如何定位不会影响其他类别的点击事件。 Basically let's say I have two classes of scroller-right, I just want to affect the class that is being click. 基本上可以说我有两个滚动条右类,我只想影响正在单击的类。

This what I have so far 这是我到目前为止

$('.scroller-right').click(function(e) {

  $('.scroller-left').fadeIn('slow');
  $('.scroller-right').fadeOut('slow');

  $('.tab-scroll').animate({left:"+="+widthOfHidden()+"px"},'slow',function(){

  });
});

$('.scroller-left').click(function() {

  $('.scroller-right').fadeIn('slow');
  $('.scroller-left').fadeOut('slow');

    $('.tab-scroll').animate({left:"-="+getLeftPosi()+"px"},'slow',function(){

    });
});    

try to use '$(this)' variable inside function liek below 尝试在下面的函数liek中使用'$(this)'变量

$('.scroller-left').click(function() {

  $('.scroller-right').fadeIn('slow');
  $(this).fadeOut('slow');


});   

You correctly observed that you need a single function. 您正确地观察到您需要一个功能。 As a result, we need to write a function which can handle both cases and add it as a handler for both. 结果,我们需要编写一个可以处理两种情况的函数,并将其添加为两种情况的处理程序。

function scrollerClick(e) {
    var isLeft = $(this).hasClass("scroller-left");
    $('.scroller-' + (isLeft ? "left" : "right")).fadeIn('slow');
    $('.scroller-' + (!isLeft ? "left" : "right")).fadeOut('slow');

    $('.tab-scroll').animate({left:"+="+widthOfHidden()+"px"},'slow',function(){

    });
}

$('.scroller-right, .scroller-left').click(scrollerClick);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM